简体   繁体   中英

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)

" : 0.0001 : 50.63 %" :0.0001 :50.63%”

how to achieve this ?

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

If you need x only as string and not object you can do the following:

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

Thanks dudes,

Answer for this query,

 var str = "X : 0.0001  Y : Globalize.format(50.635676576567, 'n2') %",
     substr;

while (str.indexOf('Globalize.format(') >= 0) {
        substr = str.substring(str.indexOf('Globalize.format('), str.indexOf(")") + 1);
        str = str.replace(substr, eval(substr));
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM