printing float to string with decimal rounding

J

Josselin

I am trying to print a float result in a string but I can get it right

I get float numbers like :
distance = 0.2568915421 , 1.2568910001, 20.3256941254 (8 decimals)

I wrote
d = (distance.*1000).round/1000.0 # to get something like 0.257
return "(- de 500m)" if d < 0.500
return "( - de 1km)" if d < 1.0
return "(env. " + d.to_s + " km)"

if d > 1.0 i print "env. 1.257 km"
I'd like to print "env. 1.2 km" shoudl I round it again ? or is there
any DRY solution ?

thanks

joss
 
F

Farrel Lifson

I am trying to print a float result in a string but I can get it right

I get float numbers like :
distance = 0.2568915421 , 1.2568910001, 20.3256941254 (8 decimals)

I wrote
d = (distance.*1000).round/1000.0 # to get something like 0.257
return "(- de 500m)" if d < 0.500
return "( - de 1km)" if d < 1.0
return "(env. " + d.to_s + " km)"

if d > 1.0 i print "env. 1.257 km"
I'd like to print "env. 1.2 km" shoudl I round it again ? or is there
any DRY solution ?

thanks

joss

Kernel#format

Farrel
 
R

Robert Klemme

I am trying to print a float result in a string but I can get it right

I get float numbers like :
distance = 0.2568915421 , 1.2568910001, 20.3256941254 (8 decimals)

I wrote
d = (distance.*1000).round/1000.0 # to get something like 0.257
return "(- de 500m)" if d < 0.500
return "( - de 1km)" if d < 1.0
return "(env. " + d.to_s + " km)"

if d > 1.0 i print "env. 1.257 km"
I'd like to print "env. 1.2 km" shoudl I round it again ? or is there
any DRY solution ?

Is this good enough?

irb(main):010:0> "%.2f" % 1.000
=> "1.00"
irb(main):011:0> "%.2f" % 1.005
=> "1.00"
irb(main):012:0> "%.2f" % 1.006
=> "1.01"

Your values

irb(main):007:0> [0.2568915421 , 1.2568910001, 20.3256941254].map {|f|
"%10.2f" % f}
=> [" 0.26", " 1.26", " 20.33"]
irb(main):008:0> [0.2568915421 , 1.2568910001, 20.3256941254].map {|f|
"%.2f" % f}
=> ["0.26", "1.26", "20.33"]

Kind regards

robert
 
J

Josselin

I am trying to print a float result in a string but I can get it right

I get float numbers like :
distance = 0.2568915421 , 1.2568910001, 20.3256941254 (8 decimals)

I wrote
d = (distance.*1000).round/1000.0 # to get something like 0.257
return "(- de 500m)" if d < 0.500
return "( - de 1km)" if d < 1.0
return "(env. " + d.to_s + " km)"

if d > 1.0 i print "env. 1.257 km"
I'd like to print "env. 1.2 km" shoudl I round it again ? or is there
any DRY solution ?

Is this good enough?

irb(main):010:0> "%.2f" % 1.000
=> "1.00"
irb(main):011:0> "%.2f" % 1.005
=> "1.00"
irb(main):012:0> "%.2f" % 1.006
=> "1.01"

Your values

irb(main):007:0> [0.2568915421 , 1.2568910001, 20.3256941254].map {|f|
"%10.2f" % f}
=> [" 0.26", " 1.26", " 20.33"]
irb(main):008:0> [0.2568915421 , 1.2568910001, 20.3256941254].map {|f|
"%.2f" % f}
=> ["0.26", "1.26", "20.33"]

Kind regards

robert

thanks a lot.. did not know about that... using too much the rails helpers...
fuunny, it's like speaking a foreign language like spanish when you
know italian... similarities but also new stuff and slang ;-))
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top