ruby - Float with 'unnecessary' digits -


i need make number out of string. use known maneuver looking this:

float(string_var) rescue nil 

this works nicely, have tiny, little problem. if string "2.50", variable 2.5. possible create float object 'unnecessary' 0 digit @ end? can literally translate "2.50" 2.50 ?

in short, answer no, given question, float, when examined, use float's to_s function, eliciting answer without trailing zeroes.

float give numeric value can interpreted way wish, though. in example, will float value (given string parsable float). asking then, how display value with trailing zeroes. that, turning float value string.

easiest way accomplish use format given 1 of respondents, namely

string_var = "2.50" float_value = float(string_var) rescue nil # 2.5 with_trailing_zeroes = "%0.2f" % float_value # '2.50' 

Comments

Popular posts from this blog

serialization - Convert Any type in scala to Array[Byte] and back -

matplotlib support failed in PyCharm on OSX -

python - Matplotlib: TypeError: 'AxesSubplot' object is not callable -