1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
|
# File 'lib/gstring.rb', line 1431
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
|