Javascript 如何找到语法错误发生的原因&;如何实现全球化格式

Javascript 如何找到语法错误发生的原因&;如何实现全球化格式,javascript,jquery,html,jquery-globalize,Javascript,Jquery,Html,Jquery Globalize,这是我的密码 Globalize.format(50.635676576567%, 'n2') 它抛出语法错误,因为已在数字中添加了%。但我需要显示输出为50.64%。如何实现通用方式{注意:符号可以是任何类似字符或特殊数字的东西…} 例如: Globalize.format(50.635676576567%, 'n2') = 50.64% Globalize.format(50.635676576567C, 'n2') = 50.64C Globalize.format(50.6356765

这是我的密码

Globalize.format(50.635676576567%, 'n2')
它抛出语法错误,因为已在数字中添加了%。但我需要显示输出为50.64%。如何实现通用方式{注意:符号可以是任何类似字符或特殊数字的东西…}

例如:

Globalize.format(50.635676576567%, 'n2') = 50.64%
Globalize.format(50.635676576567C, 'n2') = 50.64C
Globalize.format(50.635676576567@, 'n2') = 50.64@
Globalize.format($50.635676576567, 'n2') = $50.64
Globalize.format(#50.635676576567, 'n2') = #50.64
Globalize.format(50.635676576567world, 'n2') = 50.64 world

如何实现?

在应用
Globalize.format

Globalize.format(50.635676576567, 'n2')+'%';
Globalize.format(50.635676576567, 'n2')+'C';
'#'+Globalize.format(50.635676576567, 'n2');

依此类推。

您只需在输出上添加符号,即:

Globalize.format(50.635676576567, 'n2') + '%' = "50.64%"
Globalize.format(50.635676576567, 'n2') + 'C' = "50.64C"
Globalize.format(50.635676576567, 'n2') + '@' = "50.64@"
'$' + Globalize.format(50.635676576567, 'n2') = "$50.64"
'#' + Globalize.format(50.635676576567, 'n2') = "#50.64"
Globalize.format(50.635676576567, 'n2') + ' world' = "50.64 world"

请尝试此代码。必须拆分字符和数字才能执行全球化格式

var str = "Globalize.format(50.635676576567world, 'n2') Globalize.format($50.635676576567, 'n2') ",
substr;

while (str.indexOf('Globalize.format(') >= 0) {
   substr = str.substring(str.indexOf('Globalize.format('), str.indexOf(")") + 1);
   var calculate = substr.substring(substr.indexOf('(') + 1, substr.indexOf(",")),
   character = calculate.replace(/[0-9.]/g, ''),
   numberic = calculate.replace(/[^\d.]/g, ''),
   index = calculate.indexOf(character),
   formatedString = substr.replace(character, "");
   if (index == 0)
      formatedString = character + eval(formatedString);
   else
      formatedString = eval(formatedString) + character;
   str = str.replace(substr, formatedString);
}
console.log(str);

希望这对您有所帮助。

谢谢。。它可以工作,但由于某些场景,我需要使用类似于该格式的格式(Globalize.format(50.635676576567C,'n2'))如何从该格式实现。。在我的场景中,我这样转换,var format=“(Globalize.format(50.635676576567C,'n2')”;var a=eval(format);谢谢..它会工作,但由于某些场景,我需要使用类似的格式..(Globalize.format(50.635676576567C,'n2'))如何从该格式实现..在我的场景中,我这样转换,var format=”(Globalize.format(50.635676576567C,'n2'),“var a=eval(format);