|
Author |
Topic: number to string (Read 180 times) |
|
Euskadi
|
number to string
« on: Dec 28th, 2003, 5:03am » |
|
I want something that will return a fixed number of digits after the decimal, and not give me extra zeros before the decimal... with the results that if the float is huge the result will give more numbers on the left of the decimal and sacrifice some accuracy to the right. This doesn't seem to do it. Help? String numtext(float num, int dig){ int i = 0; float t = num; if(dig < 1){ dig = -1; } else { num += .5 * (1/pow(10, dig)); } do{ i++; t = t/10; } while(t >= 10); return nf(num,i,dig); }
|
|
|
|
Euskadi
|
Re: number to string
« Reply #2 on: Dec 28th, 2003, 5:05pm » |
|
I'm using nf(), but I have to beef it up since it doesn't do exactly what I want. From the nf() reference: float f = 90.012; String sf = nf(f, 3, 5); println(sf); // prints "090.01200" I don't want the extra 0 in front. Here's what I have now. I figured out that one problem I was having is related to limitations on precision for float. That was also the reason I was adding .5 before. I didn't realize that nf() rounds within its limits of precision. I would still welcome a more efficient solution. String numtext(float num, int dig){ int i = 0; float t = num; if(dig < 1){ dig = -1; } do{ i++; t = t/10; } while(t >= 10); return nf(num,i,dig); }
|
|
|
|
|