Skip to content

Instantly share code, notes, and snippets.

@alabamenhu
Created January 9, 2021 19:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alabamenhu/0667eb234edd433e3b513601b99e7d54 to your computer and use it in GitHub Desktop.
Save alabamenhu/0667eb234edd433e3b513601b99e7d54 to your computer and use it in GitHub Desktop.
Number formatter
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