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

27 posts
• Page **3** of **3** • 1, 2, **3**

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

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

**Posts:**1**Reputation:**152

B

2
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..

**Posts:**6,160**Reputation:**19,775

B

54
S

16
G

8
**Posts:**355**Reputation:**2,850

B

33
S

6
G

1
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

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)

""&floor(VAR/6)/10

I resorted to doing this:

""&floor(VAR/60)&"."&((floor(VAR/6))/10-floor((floor(VAR/6))/10))*10

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)

**Posts:**17**Reputation:**938

B

14
S

4
I have a Number type variable that gets set to a Text type variable and displayed.

It is turning an INT into a STR.

I am rounding the INT variable to show two decimal places before setting it to the STR.

I am using 'round(N * 100) / 100' as mentioned.

For the most part it is doing this, but occasionally I will get 1 or 0 decimal places.

3.49, 4.5, 5.23, 5.75, 6, 6.23

I believe it is happening when a number is an exact 4.5 or 6 and not +/- a small bit.

Anybody know how to prevent this?

****************************************

I found the answer.

https://www.scirra.com/tutorials/678/in ... -functions

You basically have to do what was mentioned above.

You have to do Math for both sides of the decimal point.

zeropad(int(time/60% 60), 2) & ":" & zeropad(int(time%60), 2)

This one is for time, I didn't test it, I just copied it from above link, it did point me towards zeropad though.

This will always result in 00:00 format.

str(floor(TestVariable/1)) & "." & str(zeropad(round(TestVariable * 100)%100,2))

I did test this one. It is for a number value that isn't time.

This will always result in 0:00 format.

If you need 00:00 or another combination then zeropad should be used.

Round(), Zeropad(,) and Modulo

https://www.scirra.com/manual/126/system-expressions

https://www.scirra.com/manual/78/expressions

It is turning an INT into a STR.

I am rounding the INT variable to show two decimal places before setting it to the STR.

I am using 'round(N * 100) / 100' as mentioned.

For the most part it is doing this, but occasionally I will get 1 or 0 decimal places.

3.49, 4.5, 5.23, 5.75, 6, 6.23

I believe it is happening when a number is an exact 4.5 or 6 and not +/- a small bit.

Anybody know how to prevent this?

****************************************

I found the answer.

https://www.scirra.com/tutorials/678/in ... -functions

You basically have to do what was mentioned above.

You have to do Math for both sides of the decimal point.

zeropad(int(time/60% 60), 2) & ":" & zeropad(int(time%60), 2)

This one is for time, I didn't test it, I just copied it from above link, it did point me towards zeropad though.

This will always result in 00:00 format.

str(floor(TestVariable/1)) & "." & str(zeropad(round(TestVariable * 100)%100,2))

I did test this one. It is for a number value that isn't time.

This will always result in 0:00 format.

If you need 00:00 or another combination then zeropad should be used.

Round(), Zeropad(,) and Modulo

https://www.scirra.com/manual/126/system-expressions

https://www.scirra.com/manual/78/expressions

**Posts:**242**Reputation:**12,787

B

68
S

37
G

13
27 posts
• Page **3** of **3** • 1, 2, **3**

## Who is online |

Users browsing this forum: Virpoja and 23 guests |