1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
|
# File 'lib/gstring.rb', line 1792
def to_eng(pa=6, unit=nil)
pa = pa.to_i
pa = (pa<1) ? 1 : (pa>15) ? 15 : pa
if self < 0.0
num = -self
sgn = "-"
else
num = self
sgn = ""
end
str = "%.16e" % num
str.(1..1)
num = str.parse('e')
exp = str.to_i
pos = (exp%3)+1
if (exp < 0)
esgn = "-"
ee = -(exp/3)*3
else
esgn = ""
ee = (exp/3)*3
end
pd = pos > pa ? pos : pa
num = (num[0..pa].to_f/10).round.to_s[0..(pa-1)].padto(pd,'0',:right)
num.insert(pos, '.') unless pos >= (num.length)
pfx = String::SI_UNIT_PREFIXES["#{esgn}#{ee}".to_i]
unless unit.nil?
unless pfx.nil?
num += pfx
ee=0
end
end
unit ||= ""
if (ee>0)
num += "e#{esgn}#{ee}"
end
return sgn+num+unit
end
|