Help with formating numbers with comma's

Get help using Construct 2

Post » Wed Jun 29, 2016 8:48 pm

I can use some help. I am trying to make a function that lets you input a number (123456789.1234) and it will output the formatted number (123,456,789.1234).
I found some older post here about it and used them and made some updates to it. But I am running into a problem when it is running. it's seems to be some kind of rounding error.
in the cap i will post you will see. i made a loop that adds 10000.33 each loop. but it starts to output numbers with 8+ decimal places. and I am not sure why.

So if anyone can take a look and see what the problem is. That would be a great help to me.
I would like to try to make this a more universal function. so that you could call it with the raw number and it would update the the text string you want.

https://www.dropbox.com/s/za0gvukgqcvukum/format_number3.capx?dl=0

Or if you know a better way to format the number I'd like to hear about it.

Thanks for your help
B
15
S
10
G
8
Posts: 107
Reputation: 4,944

Post » Wed Jun 29, 2016 10:10 pm

@Maxum
the extra digits in the decimals is just how computers handle precision. To fix that you will have to Round(NumInput*100) / 100 (or however many digits you want.

then you can format the number with one line by using RegexReplace...

set the textbox to: RegexReplace(str(NumInput),"\d{1,3}(?=(\d{3})+(?!\d))","g","$&,")


Edit: the only problem with the Regex code above is if there are more than 2 digits to the right of the decimal point, then commas will be inserted there as well.

In that case, you can use: RegexReplace(str(NumInput),"\d{1,3}(?=(\d{3})+(?=\.))","g","$&,")
but this version requires a decimal point, otherwise it wont work!

This would take some more work if you want to zero pad the decimals so there is a consistent number of digits after the decimal point.
Last edited by AllanR on Wed Jun 29, 2016 11:34 pm, edited 1 time in total.
B
94
S
33
G
20
Posts: 438
Reputation: 17,615

Post » Wed Jun 29, 2016 11:31 pm

Thanks for the help. I tried it out in this cap
https://www.dropbox.com/s/zxoh8we6dzsti29/format_number4.capx?dl=0

I need to find out more about the RegexReplace command.

Thanks
B
15
S
10
G
8
Posts: 107
Reputation: 4,944

Post » Thu Jun 30, 2016 12:30 pm

@AllanR You seem very comfartable with regex expressions. Point me to a place to learn them ?
B
33
S
18
G
27
Posts: 2,440
Reputation: 20,344

Post » Thu Jun 30, 2016 2:23 pm

@99Instances2Go

I have played around with Regex a few times - and it does come in extremely handy in many situations...

but I really understand very little of the actual syntax or how to build an expression. I usually just google what I am looking for and then copy and paste.

the trick is usually adapting the code you find to work in C2. A google search for "regex format number" will find javascript code like this:

Code: Select all
function formatNumber (num) {
    return num.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, "$&,")
}


to make that work with C2 you need to take the portion between the forward slashes to be the expression: "(\d)(?=(\d{3})+(?!\d))"
(but don't include the forward slashes).
anything after the second forward slash are the flags (in this case just "g")
(which means global or look for all occurrences - which is the most common flag).
then the part at the end is the replace string ("$&,")
(in this case it means insert the matched string and insert a comma).

so you end up with an action in C2 like this:

Set text to RegexReplace( str(num), "(\d)(?=(\d{3})+(?!\d))", "g", "$&,")
Last edited by AllanR on Thu Jun 30, 2016 2:29 pm, edited 1 time in total.
B
94
S
33
G
20
Posts: 438
Reputation: 17,615

Post » Thu Jun 30, 2016 2:28 pm

That gives me a solid direction. TyvM.
B
33
S
18
G
27
Posts: 2,440
Reputation: 20,344


Return to How do I....?

Who is online

Users browsing this forum: davidbox, Google [Bot], Mayfly, Yahoo [Bot], zulerf and 3 guests