Get numbers after . - Printable Version
+- SA-MP Forums Archive (
https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (
https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (
https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Get numbers after . (
/showthread.php?tid=608625)
Get numbers after . -
ScIrUsna - 03.06.2016
Hi,
I have float number how get what number after .?
2555.140211
How get that here is
140211
Re: Get numbers after . -
F1N4L - 03.06.2016
Floatround
Re: Get numbers after . -
Konstantinos - 03.06.2016
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
Re: Get numbers after . -
Vince - 03.06.2016
https://sampwiki.blast.hk/wiki/Floatfract
Re: Get numbers after . -
Stinged - 03.06.2016
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));
}
Re: Get numbers after . -
Infinity - 03.06.2016
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.
Re: Get numbers after . -
Stinged - 03.06.2016
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)