How do I truncate decimals to 3 decimal places

Get help using Construct 2

Post » Sun May 10, 2015 3:46 am

Below is the link to a simple project I'm doing to teach myself how to format numbers. The numbers go through a sort of case statement to figure out the end suffix of the number (this isn't my problem though if you guys know of an easier way to do what I've already done, feel free to chime in on that as well).

My problem is that I want the decimal places to be truncated down to the first 3 numbers after the decimal point (thousandths place) but I can't figure out how to do this. I've been searching the forums for awhile now and couldn't find a solution on here or in the manual so if anyone has a solution, I would be very appreciative!

https://drive.google.com/file/d/0B_fsFsHtHjNqeFFncjNrUFR6NEk/view?usp=sharing
B
5
Posts: 19
Reputation: 263

Post » Sun May 10, 2015 3:53 am

Try this:

round(Variable * 1000) / 1000
B
38
S
12
G
11
Posts: 331
Reputation: 7,712

Post » Sun May 10, 2015 4:00 am

I'm not sure how that would work with my capx... I'm already using floor to round the numbers?
B
5
Posts: 19
Reputation: 263

Post » Sun May 10, 2015 4:25 am

Well, I'm not sure at what point you are using floor but, if you always want the data rounded down, you could just replace the round function to a floor function:

floor(Variable * 1000) / 1000
B
38
S
12
G
11
Posts: 331
Reputation: 7,712

Post » Sun May 10, 2015 6:20 am

You could also use regex:
RegexMatchAt(str(yournumber),"\d*(\.\d{1,3})?","g",0)

(yournumber is the variable containing your number). If you always want 3 digits at the end, you have to rewrite it.
Visual Novel 'Engine' in 100 Events
if you ever have to choose between buying Construct 2 on scirra.com or on Steam, read this: Review
B
22
S
9
G
1
Posts: 787
Reputation: 3,786

Post » Sun May 10, 2015 6:27 am

mindfaQ wrote:You could also use regex:
RegexMatchAt(str(yournumber),"\d*(\.\d{1,3})?","g",0)

(yournumber is the variable containing your number). If you always want 3 digits at the end, you have to rewrite it.


This actually makes a lot more sense to me. Is that weird? Haha.
B
5
Posts: 19
Reputation: 263

Post » Sun May 10, 2015 6:31 am

I forgot to support minus numbers, so it could be this instead, probably:
RegexMatchAt(str(yournumber),"-?(0|([1-9]\d*))(\.\d+)?","g",0)
Visual Novel 'Engine' in 100 Events
if you ever have to choose between buying Construct 2 on scirra.com or on Steam, read this: Review
B
22
S
9
G
1
Posts: 787
Reputation: 3,786

Post » Sun May 10, 2015 6:32 am

I actually have no need for negative numbers but if I ever do, this will come in handy. Haha.
B
5
Posts: 19
Reputation: 263


Return to How do I....?

Who is online

Users browsing this forum: combotg, Lancifer and 8 guests