How do I format numbers?

Get help using Construct 2

Post » Mon Jun 05, 2017 11:46 pm

How do I format numbers?
For example I want to do something like "%5.2f".

5.1, 12.05 and 3.006 should be " 5.10", "12.05" and " 3.01". keep in mind the spaces.

I know how to work around that, but it doesn't seem elegant enough.

My problem might seem similar to:
expression-needed-to-round-number_p1015301?#p1015301
but it's not the same ^^ I don't want "at most" 2 decimal points. I want it to be constant. round(number*100)/100 won't work, because 1 would be still 1, not 1.00, also it doesn't help me with spaces.
The creator of Gurgosus card universe!
Visit Gurgosus.eu for more info.
B
18
S
2
G
2
Posts: 50
Reputation: 1,991

Post » Tue Jun 06, 2017 12:29 am

The zeropad() expression is helpful here. Example:

var=5.1

text = int(var) &"."&zeropad(round((var%1)*100), 2)

Could have issues with a number like 1.995 though. Another option is some plugin ( the forum has a listing in the plugin section that can be looked through) or using some javascript through the browser object's browser.execjs() expression. Example:

set text to browser.execjs("parseFloat(Math.round("& var &" * 100) / 100).toFixed(2);")
B
94
S
33
G
114
Posts: 5,362
Reputation: 73,785

Post » Tue Jun 06, 2017 12:43 am

Construct 2 doesn't have any formatting helper... you need to create your own formatting expression.

I guess this could work :
Code: Select all
right(" " & floor(number), 2) & "." & zeropad(round(number * 100) % 100, 2)
B
75
S
31
G
27
Posts: 1,000
Reputation: 20,735


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 10 guests