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
16
S
1
G
1
Posts: 45
Reputation: 1,316

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
91
S
31
G
103
Posts: 5,234
Reputation: 67,754

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
67
S
28
G
24
Posts: 970
Reputation: 18,675


Return to How do I....?

Who is online

Users browsing this forum: Alvaro2, Yahoo [Bot] and 16 guests