最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to check if a number has two decimal places and keep the trailing zero if necessary? - Stack Overflow

programmeradmin0浏览0评论

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 to 2 – Patrick Evans Commented Oct 5, 2013 at 0:03
 |  Show 3 more ments

3 Answers 3

Reset to default 2

but 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>

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论