Search code examples
javascriptjqueryglobalization

How to convert a globalize format from string


I have a following code,

  var x = "X : 0.0001  Y : Globalize.format(50.635676576567, n2) %"

x is a string.

I need to show the x value as ,(need to convert as globalize format)

"X : 0.0001 Y : 50.63 %"

how to achieve this ?


Solution

  • You can achieve this, parse the string and eval() method, refer below code snippet.

    var x = "X : 0.0001  Y : Globalize.format(50.635676576567, 'n2') %"
    var substring = x.substring(x.indexOf('Y') + 4, x.length - 2);
    // assign the value after replacing the string
    x = x.replace(substring, eval(substring));
    console.log(x);
    

    But this you need to assign n2 in single quotes or else you can add single quotes by code, for this refer this link