Created
June 7, 2020 07:46
-
-
Save noahgibbs/e0a9c1c0d1094762a69ade49f41cb6c8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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