Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert exponentials to decimal in ruby

Tags:

ruby

I have output like the following exponential numbers.

6.0e-07

8.1e-07

1.1e-09

But I want above numbers should be displayed like below

0.00000060

0.00000081

0.0000000011

I mean in the form decimal format. I surfed in the net. I could not find any solution for this.

Is it possible in ruby?. if yes How to do that?.

like image 283
Sam Avatar asked Mar 27 '26 14:03

Sam


1 Answers

You could use BigDecimal#to_s:

require 'bigdecimal'

BigDecimal.new('6.0e-07').to_s('F') #=> "0.0000006"
BigDecimal.new('8.1e-07').to_s('F') #=> "0.00000081"
BigDecimal.new('1.1e-09').to_s('F') #=> "0.0000000011"
like image 99
Stefan Avatar answered Mar 29 '26 06:03

Stefan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!