Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/418.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript格式第2位,然后第3位表示100%_Javascript_Jquery - Fatal编程技术网

javascript格式第2位,然后第3位表示100%

javascript格式第2位,然后第3位表示100%,javascript,jquery,Javascript,Jquery,我有一个jQuery插件进度表,它可以设置动画,并且有一个回调函数,可以将文本值设置为最大值 回调值的格式为0.12798798等,我的格式为12% 我的问题是如果值为100%,我的代码返回00% $('#circle').circleProgress({ startAngle: 90, size: 240, thickness: 22, fill: { gradient: [['#0681c4', .5], ['#4ac5f8', .5]], gradient

我有一个jQuery插件进度表,它可以设置动画,并且有一个回调函数,可以将文本值设置为最大值

回调值的格式为0.12798798等,我的格式为12%

我的问题是如果值为100%,我的代码返回00%

$('#circle').circleProgress({
    startAngle: 90,
    size: 240,
    thickness: 22,
    fill: { gradient: [['#0681c4', .5], ['#4ac5f8', .5]], gradientAngle: Math.PI / 4 }
}).on('circle-animation-progress', function(event, progress, stepValue) {
    $(this).find('strong').text(String(stepValue.toFixed(2)).substr(2)+'%');
});

我相信有更好的方法。

您可以简化如下:

(stepValue*100).toFixed(0) + '%'

看起来应该四舍五入到最接近的整数:

Math.round(0.12798798*100)
toFixed(2)给出两位小数。。。使用0,则不需要substr

 $(this).find('strong').text(String(stepValue.toFixed(0))+'%');

非常感谢,好多了。如果您添加它作为答案,我将接受它。这不包括%或类型转换。这只是为了缩短
字符串(stepValue.toFixed(2))。substr(2)
。我现在已经添加了
%
,但我确信OP已经知道要这么做了。而且,在这种情况下不需要类型转换
toFixed()
自动返回字符串。