Get numbers after .
#1

Hi,

I have float number how get what number after .?


2555.140211

How get that here is

140211
Reply
#2

Floatround
Reply
#3

2 ways I can think of.

1)
pawn Код:
new
    Float: a = 2555.140211,
    b[16],
    c;

format(b, sizeof b, "%f", a);
sscanf(b, "p<.>{i}i", c);
// c = 140211
You may have noticed {i} that the silent speficier that ignores the first integer before dot which is 2555 in our case. "p" specifier splits with delimiter (inside <delimiter_here>) which is dot.

sscanf is recommended.

2)
pawn Код:
new
    Float: a = 2555.140211,
    b[16],
    c;

a = floatfract(a); // a = 0.140211
format(b, sizeof b, "%f", a);
c = strval(b[2]);
// string b[2] is "140211"
// c = 140211
Reply
#4

https://sampwiki.blast.hk/wiki/Floatfract
Reply
#5

If you want it as an integer, you can use this:
Код:
abs(floatround(value * 100.00) - (floatround(value, floatround_tozero) * 100));
Here's the function for abs:
Код:
abs(value)
{
	return ((value < 0) ? (-value) : (value));
}
Reply
#6

Quote:
Originally Posted by Stinged
Посмотреть сообщение
If you want it as an integer, you can use this:
Код:
abs(floatround(value * 100.00) - (floatround(value, floatround_tozero) * 100));
Here's the function for abs:
Код:
abs(value)
{
	return ((value < 0) ? (-value) : (value));
}
No, this should not be used. You hardcoded it to only obtain two numbers of the fraction. Vince's solution is superior to this. If you really want it as an integer, cast floatfract's result to an int.
Reply
#7

Oh yeah sorry, I didn't think about it, not even for a second.
I actually made that so I could have cents ($0.50 for example)
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)