2

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 ?

Zumry Mohamed
  • 9,318
  • 5
  • 46
  • 51
Akbar Basha
  • 1,168
  • 1
  • 16
  • 38

3 Answers3

2

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

Community
  • 1
  • 1
mani
  • 96
  • 11
1

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 + "%"
Shhade Slman
  • 263
  • 2
  • 10
  • it can be work. but i need to customized from string.."X : 0.0001 Y : Globalize.format(50.635676576567, n2) %" – Akbar Basha Nov 23 '15 at 09:47
  • Maybe the following link can help you : http://stackoverflow.com/questions/359788/how-to-execute-a-javascript-function-when-i-have-its-name-as-a-string – Shhade Slman Nov 23 '15 at 11:08
1

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));
    }
Akbar Basha
  • 1,168
  • 1
  • 16
  • 38