Help with formating numbers with comma's

0 favourites
  • 7 posts
From the Asset Store
Game with complete Source-Code (Construct 3 / .c3p) + HTML5 Exported.
  • 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

  • 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.

  • 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

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • AllanR You seem very comfartable with regex expressions. Point me to a place to learn them ?

  • 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:

    function formatNumber (num) { 
        return num.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, "$&,") 
    } 
    [/code:dm8cb1vx] 
     
    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", "$&,")
  • That gives me a solid direction. TyvM.

  • Sweet, thank you. Worked great.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)