-
-
Save alabamenhu/0667eb234edd433e3b513601b99e7d54 to your computer and use it in GitHub Desktop.
Number formatter
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
sub ($number) { | |
return "NaN" if $number === NaN; | |
return "∞" if $number === Inf; | |
return "-∞" if $number === -Inf; | |
my str $result; | |
my $n = abs $number; | |
my constant @digits = array[str].new("0", "1", "2", "3", "4", "5", "6", "7", "8", "9"); | |
my int $power = ceiling log10 $n; | |
$power max= 1; | |
my $int-val = $n.Int; | |
my $magnitude = 10 ** $power; | |
my int $digit; | |
for ^$power { | |
$magnitude = $magnitude div 10; | |
$digit = $int-val div $magnitude; | |
$int-val -= $digit * $magnitude; | |
$result ~= @digits[$digit]; | |
} | |
return $result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment