Skip to content

Instantly share code, notes, and snippets.

@noahgibbs
Created June 7, 2020 07:46
Show Gist options
  • Save noahgibbs/e0a9c1c0d1094762a69ade49f41cb6c8 to your computer and use it in GitHub Desktop.
Save noahgibbs/e0a9c1c0d1094762a69ade49f41cb6c8 to your computer and use it in GitHub Desktop.
# Change a sci-notation string to a float string without using the Numeric class in between. At all.
# Assume input string in "12345e7" format,
# return string in "123450000000.0" format
def exp2sf(exp_str)
mant, ex = exp_str.split("e")
zeroes = ""
next_place_zeroes = "0"
# From least-significant to most-significant
ex.reverse.each_char do |ex_digit|
this_place_zeroes = next_place_zeroes
next_place_zeroes = next_place_zeroes + next_place_zeroes +
next_place_zeroes + next_place_zeroes +
next_place_zeroes + next_place_zeroes +
next_place_zeroes + next_place_zeroes +
next_place_zeroes + next_place_zeroes
("1"..ex_digit).each { zeroes += this_place_zeroes }
end
mant + zeroes + ".0"
end
puts exp2sf("123e5")
puts exp2sf("123e47")
puts exp2sf("123e47").length
puts exp2sf("123e421")
puts exp2sf("123e421").length
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment