'Radix Conversions
I've just managed to Number.prototype.toString(radix) also with floating point numbers. Regular Integers are converted to Strings this way:
var rest = Math.floor(this.valueOf()); // ignoring the -/+ in this example
var result = '';
while(rest >= radix)
{
result = alphabet[rest % radix] + result;
rest = Math.floor(rest / radix);
}
if(rest >= 1) result = alphabet[rest] + result;
Now I've appended this for possible floating points:
var rest = this.valueOf() % 1;
var zeros = 0;
if(rest === 0) return result;
else while(rest % 1 !== 0) // => "is float?"
{
rest *= radix;
// leaving out the initial 0 count, as counted in the variable 'zeros'
}
result += '.' + String.repeat(zeros, alphabet[0]) + rest.toString(radix);
But my result strings for floating points are too large see this example screenshot.
So, what is/are the additional while() statement(s)? My regular code is here:
from line 314 and below.
MAYBE there's a way to handle the floating points in parallel to
and within the first (int-)while() [so one side /=, the other *=, or smth. similar]?
Thank you.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
