Round / Format a decimal value

Get help using Construct 2

Post » Tue Dec 01, 2015 7:23 am

Message: jeryymanly is not yet permitted to post plain text URLS (300 rep required). 1 URLS removed. Why?
float

float f = 10.123456F;
float fc = (float)Math.Round(f * 100f) / 100f;

Double

Double d = 100.123456;
Double dc = Math.Round((Double)d, 2);

Decimal

decimal d = 100.123456M;
decimal dc = Math.Round(d, 2);

Source : Jeery
B
2
Posts: 1
Reputation: 152

Post » Tue Dec 01, 2015 2:47 pm

jeryymanly wrote:float

float f = 10.123456F;
float fc = (float)Math.Round(f * 100f) / 100f;

Double

Double d = 100.123456;
Double dc = Math.Round((Double)d, 2);

Decimal

decimal d = 100.123456M;
decimal dc = Math.Round(d, 2);

Source : Jeery


Any reason for this reaction in this thread?
These aren't Construct2 expressions, so I'm just curious..
I told my dentist I had trouble with my teeth and asked her to fix it without looking in my mouth..
B
53
S
16
G
8
Posts: 6,157
Reputation: 19,744

Post » Sat Aug 13, 2016 2:06 am

How do I add this round(N * 100) / 100) to a button in Construct 2?
Make a global var N?
Or does it get attached to result?


Nevermind I got it.
B
29
S
13
G
1
Posts: 41
Reputation: 2,898

Post » Tue Oct 04, 2016 2:57 pm

Great thread, helped a lot!
Image
B
14
S
4
G
1
Posts: 268
Reputation: 1,794

Post » Tue Oct 04, 2016 10:52 pm

Hi guys its possible to make counter like this 00:00 ?
B
14
S
5
Posts: 109
Reputation: 1,366

Post » Wed Oct 05, 2016 10:18 am

Well I thought I'd share a timer with 1 decimal place I made. I found that Construct 2 rounded off a number like 2.0 to just 2, which made the text jumpy. So instead of doing
""&floor(VAR/6)/10
I resorted to doing this:
""&floor(VAR/60)&"."&((floor(VAR/6))/10-floor((floor(VAR/6))/10))*10 :lol:

The idea is that it finds two different numbers, one for the number of seconds remaining, and one for the number of 10ths of a second remaining in that second, then you insert a decimal point "." in the middle of the two numbers.

So lets say you wanted to round a number of seconds into a format like 3.5 hours. First, you need to find out how many full hours are left. You can calculate that by rounding down the 'number of seconds divided by 3600', that is, floor(no_of_seconds/3600). You then need to find out how many 10ths of an hour are left in that hour, which is quite a bit more complicated. You need to find out the remainder of seconds when divided by 3600 - no_of_seconds-(floor(no_of_seconds/3600))*3600. Then you need to find out how many 10ths of an hour (360 seconds) that many seconds equate to - floor((no_of_seconds-(floor(no_of_seconds/3600))*3600)/360). And finally to finish it off, you string it together to get:
""&floor(no_of_seconds/3600)&"."&floor((no_of_seconds-(floor(no_of_seconds/3600))*3600)/360) :D
B
14
S
3
Posts: 17
Reputation: 788

Previous

Return to How do I....?

Who is online

Users browsing this forum: No registered users and 25 guests