js中,需要把:
1 2 3 4 5 6 7 8 9 10 | memberInfo : { 'username' : 'Maggie' , 'totalScriptCount' : 121, 'publishedScriptCount' : 100, 'publishedScriptRatio' : 0.82644 , "unpublishedScriptCount" : 0, 'unpublishedScriptRatio' : 0.173553, }, |
中的
0.82644
0.173553
变成:
“82.6”
“17.4”
js float to string format
1 2 3 4 5 | const percentValue = text * 100 console.log( "percentValue=" , percentValue) const oneDigitValueStr = percentValue.toPrecision( 1 ) console.log( "oneDigitValueStr=" , oneDigitValueStr) return `${oneDigitValueStr} % ` |
竟然输出:
1 2 3 4 | percentValue = 82.64 AllUserScriptCount.js: 77 oneDigitValueStr = 8e + 1 AllUserScriptCount.js: 75 percentValue = 17.349999999999998 AllUserScriptCount.js: 77 oneDigitValueStr = 2e + 1 |
【总结】
换用代码:
1 2 3 4 5 6 | const percentValue = text * 100 console.log( "percentValue=" , percentValue) / / const oneDigitValueStr = percentValue.toPrecision( 1 ) const oneDigitValue = percentValue.toFixed( 1 ) console.log( "oneDigitValue=" , oneDigitValue) return `${oneDigitValue} % ` |
结果对了:
1 2 3 4 5 | percentValue = 82.64 AllUserScriptCount.js: 78 oneDigitValue = 82.6 AllUserScriptCount.js: 73 text = 0.1735 , ... AllUserScriptCount.js: 75 percentValue = 17.349999999999998 AllUserScriptCount.js: 78 oneDigitValue = 17.3 |
显示也对了:
