Help converting numbers.



  • Hey everyone I'm just getting started with OB and I'm having trouble converting numbers like 1970 to 1,970 or 20768 to 20,768.

    I tried using the compute function but it adds a dot instead of a comma and removes the 0 at the end.


  • Donator

    @Heisenberg

    Hello Heisenberg,

    i always did this with javascript maybe there is a way to do it with the function blocks but i dont know.

    Here is my example code if you want to do it in Javascript:

    FUNCTION Constant "10000000" -> VAR "testnumber" 
    
    BEGIN SCRIPT JAVASCRIPT
    
        var change_this_to_variable_name_you_want = testnumber.toString().replace(/\B(?<!\.\d*)(?=(\d{3})+(?!\d))/g, ",");
    END SCRIPT -> VARS "change_this_to_variable_name_you_want"
    

    var testnumber is the variable where your plain number is stored you need to change it in line 5 to the variable where your number is stored
    var change_this_to_variable_name_you_want is the variable name that will have your number with the thousands seperator change the variable name in line 5 and line 7 as you wish
    and of course delete the first function block it was only to create a test number 😄

    b8d934ec-f326-4fbc-935e-0dc89d87d4e3-image.png


  • Admin

    You could also replace the commas with nothing before passing them to the compute block


Log in to reply