admin管理员组文章数量:1335832
If you have these numbers:
2
2.05
2.547
2.5
How can I do a check to see if the number has two decimal places?
If it has 2 decimals, how do you drop the second decimal number (rounding) but keep the trailing 0 if needed?
Examples of results:
2.05 would end in 2.1
2.04 would end in 2.0
2.54 would end in 2.5
I realise you can use toFixed(1), but that converts it to a string. And if you convert it to a int with parseInt(), it loses the trailing zero
Thanks guys
If you have these numbers:
2
2.05
2.547
2.5
How can I do a check to see if the number has two decimal places?
If it has 2 decimals, how do you drop the second decimal number (rounding) but keep the trailing 0 if needed?
Examples of results:
2.05 would end in 2.1
2.04 would end in 2.0
2.54 would end in 2.5
I realise you can use toFixed(1), but that converts it to a string. And if you convert it to a int with parseInt(), it loses the trailing zero
Thanks guys
Share Improve this question edited Oct 5, 2013 at 0:00 user2413333 asked Oct 4, 2013 at 23:54 user2413333user2413333 1,4054 gold badges18 silver badges22 bronze badges 8- 1. round 2. format as necessary – zerkms Commented Oct 4, 2013 at 23:57
-
1
use
.toFixed
, it rounds the float to the number of decimal places specified. – Patrick Evans Commented Oct 4, 2013 at 23:57 - possible duplicate of How do you round to 1 decimal place in Javascript? – sushain97 Commented Oct 4, 2013 at 23:58
- 2 it would have to be a string you cant have a float that keeps the trailing 0 as trailing 0's are meaning less in floats which is why they are always dropped. – Patrick Evans Commented Oct 5, 2013 at 0:02
-
1
why would you use parseInt, you should be using parseFloat. parseInt parses a string to an integer, and integers have no decimal parts. Even then
2.0
would still be parsed to2
– Patrick Evans Commented Oct 5, 2013 at 0:03
3 Answers
Reset to default 2but toFixed turns it into a string, and if you use parseInt it gets rid of the trailing zero
You can have a number or you can have a representation of a number, but there is no third alternative.
If you want a representation of a number, what's wrong with a string? If you want a number, then there is no "trailing zero".
The number represented by "2.0" doesn't have a trailing zero because only representations can have trailing zeros. The number two can be represented with or without a trailing zero and it's the same number.
So: Do you want a number or a representation?
I think you must want a representation, because really only representation can have some specific number of decimal places. In that case, why isn't toString
the right solution?
Assuming that x
is the value you are dealing with
var temp = Math.round(x * 10) / 10; // This is the value as a number
document.write(temp.toFixed(1)); // writes the number in the desired format
In JavaScript, there is no distinction between the value 1.5
and the value 1.50
- as numbers, they are exactly the same. It is only when you convert them to a string, using something like toFixed()
, that they bee distinct.
I'd suggest (the HTML upon which this is based follows):
$('li').text(function(i,t){
var rounded = t.split('.')[1] ? (Math.round(parseFloat(t) * 10)/10).toFixed(1) : t;
return t + ' (' + rounded + ')';
});
JS Fiddle demo.
The ternary:
t.split('.')[1] ? (Math.round(parseFloat(t) * 10)/10).toFixed(1) : t;
Assesses whether there's a period, if so then we parse the float to a number, multiply it by 10 (because we want the number to one decimal place), round that number and then divide it by ten and then, with toFixed(1)
truncate the number to one (correctly-rounded) decimal place.
If there's no decimal portion to the number (it's an integer, not a float) we do nothing with it and simply use the existing number as it was found.
This is based upon the following HTML:
<ul>
<li>2</li>
<li>2.05</li>
<li>2.547</li>
<li>2.5</li>
<li>2.04</li>
</ul>
本文标签:
版权声明:本文标题:javascript - How to check if a number has two decimal places and keep the trailing zero if necessary? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742398503a2467407.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论