如何在Javascript中使用零结尾的字符串长度? [英] How to take the length of a string with zero at the end in Javascript?
本文介绍了如何在Javascript中使用零结尾的字符串长度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在Javascript结尾处取零字符串的长度?
如果字符串的值为123.45。长度是5,这很好。
但是如果字符串的值是123.40。长度为4而不是5.
解决方案
1。回复评论,以便通知用户。
2.点击改进问题并将代码放在实际问题而不是评论中。
3.你可以'这样做是因为123.40与123.400和123.4000具有相同的精确值,因此您必须将其放入字符串或使用那么多地方对其进行格式化。例如,您可以这样做:
var num = 123 。 40 ;
num = num.toFixed( 2 ); // 这将确保它总是有2个小数位,以便以0结尾。
How to take the length of a string with zero at the end in Javascript?
if the value of the string is as "123.45". The length is coming as 5 which is fine.
But if the value of the string is as "123.40". The length is coming as 4 instead of 5.
解决方案
1. Reply to comment so that the user is notified.
2. Click improve question and put your code in the actual question instead of in a comment.
3. You can't do it like that because 123.40 is the same exact value as 123.400 and 123.4000 so you'll have to put it into a string or format it with that many places. For example you can do:
var num = 123.40; num = num.toFixed(2); // this will make sure it always has 2 decimal places so that ending in 0 is accounted for.
这篇关于如何在Javascript中使用零结尾的字符串长度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文