0

这是我的代码,

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

如何实现?

4

3 回答 3

1

您只会在输出中添加符号,即:

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"
于 2015-11-25T05:48:37.933 回答
1

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

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);

希望这会帮助你。

于 2015-11-25T06:15:08.493 回答
0

您可以在应用后简单地添加必要的符号Globalize.format

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

等等。

于 2015-11-25T05:48:17.663 回答