How do I use .2f on Web Storage?

Get help using Construct 2

Post » Wed Apr 16, 2014 1:04 am

We're trying to save the best score, but it's a float number. When we compile it we get a NaN because the number is too long. How do we save only 2 number after the comma? For actual result and best result?
B
12
S
3
G
1
Posts: 188
Reputation: 1,377

Post » Wed Apr 16, 2014 1:26 am

floor(random(3)+1)

Is an example from my .capx

Random returns a float value but putting floor(value+1) makes it so it rounds down to the nearest whole value.

Or you could try round(x) but I think both of those can only round to the nearest whole number and not any certain decimal place.
B
8
S
1
Posts: 76
Reputation: 949

Post » Wed Apr 16, 2014 1:47 am

Think of it this way, if you have a number:
3.14159265359

If you want to trim down the precision of this floating point number, what you do is fairly simple.

Let's say you want only 5 decimal places, you first multiply this number by 1*10^5, or, 100,000:
314159.265359

Now you round it to the nearest whole number with the round operator.
314159

And finally divide it by that same 1*10^5 (100,000):
3.14159

And voila, you have trimmed off a few decimal places. You can even do this all in one handy equation:
(round(3.14159265359*100000))/100000

If someone could only clarify whether or not the float() operator needs to be used. This above is a pseudo implementation, but in theory that's exactly what you would do to trim your precision.
ImageImageImageImage
B
62
S
19
G
51
Posts: 633
Reputation: 30,836


Return to How do I....?

Who is online

Users browsing this forum: blackhornet and 5 guests