def shift(num,exp) num = num.split(//) if exp < 0 dotpos = 0 num.each_with_index do |k,v| dotpos = v if k == "." end if exp.abs > dotpos (exp.abs - dotpos).times do num.insert(0,"0") end num.delete_if {|x| x == "."} num.insert(0,".") else num.delete_if {|x| x == "."} num.insert(dotpos - exp.abs,".") end else dotpos = 0 num.each_with_index do |k,v| dotpos = v+1 if k == "." end exlen = num.length-dotpos num.delete_if {|x| x == "."} (exp-exlen).times do num.insert((num.length),0) end end num.join() end def to_float(num) num = num.split(//) orignum,exp = "","" endex,startex = 0,0 num.each_with_index {|k,v| endex = v and break if k == "x" or k == " "} num.each_with_index {|k,v| startex = v and break if k == "^"} num.each_with_index {|k,v| orignum << k if v < endex } num.each_with_index {|k,v| exp << k if v > startex} return 10 ** exp.to_i if orignum == "1" return shift(orignum,exp.to_i) end def to_sci(num) num = num.to_s.split(//) if num.join.to_f > 1 orignum,explen = "",0 startget = false num.reverse.each {|x| startget = true if x != "0"; orignum << x if startget} orignum = orignum.reverse orignum = orignum.split(//).insert(1,".").join() return orignum << " x 10^" << (num.length-1).to_s else return num.to_s << " x 10^0" if num.join().to_f == 1.0 dotpos,fnumpos,explen = 0,0,0 orignum = "" num.each_with_index {|k,v| dotpos = v if k == "."} num.each_with_index do |k,v| if k != "0" and k != "." fnumpos = v break end end num.each_with_index {|k,v| orignum << k if v >= fnumpos} orignum = orignum.split(//).insert(1,".").join() num.each_with_index {|k,v| explen += 1 if dotpos < v and v <= fnumpos} return orignum << " x 10^" << (explen*-1).to_s end end puts to_sci("632194030000") puts to_float("6.3219403 x 10^11")