# Garbage float doubles tostring

Does anyone have a good algorithm for converting floats and doubles into characters directly now i found a way to bypass it with longs and ints. With just a straight out modulous routine.

But with doubles and floats the approximated representation screws up the modulous operation at about 1/1000 ths place well about half the time.

Figured maybe someone has done this already and has the algorithm.

It is usually a bad idea to convert floats and doubles (and values in general) to a string each frame.

Instead i would check if the new value is different from the previous and only then convert to a string.
Converting values which do not change between many (or even 2?!) frames is not worth.

Anyway i don’t have the algorithm. Maybe use the old system of saving floats into ints and shifting a pointer to simulate the mantissa.

Garbage-free StringBuilder… with warnings that it is poking around inside the internals.

Garbage-free StringBuilder3… with warnings that it is poking around inside the internals.

It still seems to generate garbage just slowly at least something is.
You have to turn off fixed time step to see it.

Id rather use a math routine to directly turn a number into a char array. I don’t want the number on the heap at all period so i can be sure its on the stack at the point stringbuilder gets it.

something like the below

``````        int n = 255; // the number to change to chars
int p = 100;
while (p > 0)
{
if (n >= p)
{
builder.Append((char)((int)((int)(n % (10 * p)) / p) + 48));
}
p = (int)(p * .01);
}
``````

This sort of thing works cause char is a struct and it’s fine for int’s longs bytes but,
it wont work on floats doubles.

By that i mean it does, but, at about the .0001 place. it might no longer match what Tostring would output plus float double can express scientific numbers i think, i have no idea were to find the exact algorithm.

Im trying to make a little stringbuilder hack via a wrapper and extensions to it but im just missing the exact code formula for float double.

re: ah wait a minute i might have a stupid simple solution.

Maybe regexps will help you there with capturing with a simple one like: (\d+).(\d+) to get integer and fractional part of a decimal, in its general meaning, number.
See System.Text.RegularExpressions namespace.

ok i think i see whats wrong but i don’t see how ToString gets around it.

Either the mod operator or double multiplication in decimal is adding ever so slightly to my incrementing modulator

such that if

n = .1;
q =.1;

for(int i = 0; i < 15; i++)
{
q = q * n;
}
Then
n will not be exact at some very small decimal point on the third iteration
it will be like .0010000000000000003
Then
When that is eventually modulated into some value it changes it ever so slightly to a number just short of what it should be this gives the appearance of a large change when skews say a 1 into a 0 and adds a 9 after that.
for example .302010 might become .3020099 ect.

If any of you guys see a way to get around this feel free to take a shot.

``````    public static void AppendMgDouble(this StringBuilder builder, double number)
{
// basics
if (number < 0)
{
// Negative.
builder.Append(minus);
number = -number;
}
//else
//{
//    // Positive
//    builder.Append(plus);
//}
if (number == 0)
{
builder.Append('0');
return;
}
``````
``````        long place = 100000000000000L;
if (number >= place*10)
{
// just append it, if its this freggin huge, this is not a science calculator.
builder.Append(number);
return;
}
// part 1 pull integer digits
long n = (long)(number);
while (place > 0)
{
if (n >= place)
{
long modulator = place * 10L;
long val = n % modulator;
long dc = val / place;
builder.Append((char)(dc + 48));
}
else
{
}
place = (long)(place * .1);
}
``````
``````        // ok lets try again
double nd = number - (double)(n);
if (nd > 0 && nd < 1) { builder.Append(adecimal); }
nd = number;
double placed = .1;
while (placed > 0.0000001)
{
if (nd > placed)
{
double modulator = placed * 10;
double val = nd % modulator;
double dc = val / placed;
builder.Append((char)(dc + 48));
}
else
{