Round / Format a decimal value

Get help using Construct 2

Post » Tue Nov 03, 2015 9:58 am

round(x) is only for x= float value, if you try to round an integer, round(int) returns int
Image Image
B
13
S
5
G
100
Posts: 848
Reputation: 51,725

Post » Tue Nov 03, 2015 10:58 am

Kenthria wrote:For me i'm doing round(AResourceValue * 1000000) / 1000000- and if the ARV is 1,345,657 the end value is the same.


Seeing your expression it seems you want to be able to have 6 numbers behind your decimal point..

if you have a value of 1.5123456 it will become 1.512346 using this expression, so it works..
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 » Tue Nov 03, 2015 11:06 am

No just 3, so it would eventually be 1.512m rather than 1.5123456.
B
21
S
8
G
1
Posts: 83
Reputation: 2,194

Post » Tue Nov 03, 2015 11:26 am

Kenthria wrote:No just 3, so it would eventually be 1.512m rather than 1.5123456.


Than this should work:

round(AResourceValue * 1000) / 1000
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 » Tue Nov 03, 2015 11:45 am

It doesn't but I just heard that the value cant be a whole number.
So I think round((AResourceValue / 1000000) * 1000)) /1000
should work.
B
21
S
8
G
1
Posts: 83
Reputation: 2,194

Post » Tue Nov 03, 2015 11:53 am

Kenthria wrote:It doesn't but I just heard that the value cant be a whole number.
So I think round((AResourceValue / 1000000) * 1000)) /1000
should work.


Well, yes..
The expression was made to limit the amount of decimal numbers on a number with decimals..

If you don't have any decimals, ofcourse it won't work..
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 » Sun Nov 08, 2015 3:59 am

Works great unless you want to display zeroes on the end of your decimal placing, for example making a field that enters a price (dollar value) for something.

Any idea how to easily force a zero onto the end of decimal?

~Sol
Tired of crappy file hosts that are crappy? Get DROPBOX - https://db.tt/uwjysXJF
Moderator
B
45
S
17
G
37
Posts: 2,853
Reputation: 25,966

Post » Sun Nov 08, 2015 5:28 am

@SoldjahBoy
You can do that with the zeropad() expression.
int(number)&"."&zeropad(int(number*100)%100, 2)

That only works for positive numbers though. For negative numbers make it positive first, then add a minus sign after. Using a function would be useful in this case.

Or if you add the browser object to your project you can do it with js:
Browser.ExecJS(number&".toFixed(2)")
B
79
S
24
G
52
Posts: 4,725
Reputation: 39,713

Post » Sun Nov 08, 2015 6:06 am

@R0J0hound
Thanks for that mate, I'll give it a try and see how it works. What I'm trying to do is have a text input box where it sets the decimal place on the fly, as the user inputs a number.

For example, the user types in 2350 and the box will display 23.50, with two decimal places always being shown during the input; IE with the example number provided, as the user enters the first digit "2" the box will show 0.02... then when the "3" is added it will display 0.23, then the "5" will make it 2.35, and lastly the zero should show 23.50

So far simply using the example provided by Ashley works well, until a "0" is present at the end of the input (because the 0 gets ignored unless followed by another number).

I'll play around with your example and see if I can get it to play nicely. Cheers!

*EDIT*
Ended up as this for a working result:
Image
Seems pretty solid!

~Sol
Tired of crappy file hosts that are crappy? Get DROPBOX - https://db.tt/uwjysXJF
Moderator
B
45
S
17
G
37
Posts: 2,853
Reputation: 25,966

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

PreviousNext

Return to How do I....?

Who is online

Users browsing this forum: mercuryus, mystazsea and 19 guests