Removing zeros from the start of a float

   128   1   1
User Avatar
Member
22 posts
Joined: Nov. 2019
Offline
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!
User Avatar
Member
302 posts
Joined: Jan. 2013
Offline
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));
Edited by alexwheezy - yesterday 12:32:51
  • Quick Links