Search - User list
Full Version: Removing zeros from the start of a float
Root » Technical Discussion » Removing zeros from the start of a float
TubeSmokeGuy
Hi!

I try to find a way to remove all zeros of a float before the first number which isn't a zero.
The float is a prim attribute.

Ex.:

0.000000000135 -> 135
0.456 -> 456
0.0000567 -> 567
0.01 -> 1

The count of zeros is variable.


Any idea?


Thank you and greetings!
alexwheezy
You can do it this way:

// 0.000000000135 -> 135
// 0.456 -> 456
// 0.0000567 -> 567
// 0.01 -> 1

float input = 0.01;
float value = input * pow(10, abs(ceil(log10(input))));

int n = -1;
float eps = 1e-9;
while(value - floor(value) >= eps) {
    value *= 10;
    ++n;
}
int result = int(value);

or

float input = 0.01;
float value = input * pow(10, abs(ceil(log10(input))));
int result = select(value < 1.0, atoi(sprintf("%g", value)[2:]), int(value));
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB