1

I would like your help with the following: I have prices which could be more then 2 decimal points, i.e. 0.009 or 0.0014

However, I also have prices which have regular prices like 2.9 or 14.45.

I store all prices up until 4 decimal points i.e. 2.9 becomes 2.9000.

What I'm looking for is the following, I want to show at least 2 decimal points num.toFixed(2), however if there are more then 2 decimal points and those are not 0 I want to show them too.

Example:

  • 2.9000 becomes 2.90
  • 0.0001 remains 0.0001
  • 2.8540 becomes 2.854
  • 1.0100 becomes 1.01
  • 3.0000 becomes 3.00

I've tried num.toFixed(2) but this removes all decimals after the 2nd.

How can I achieve what I've shown in the example?

Thanks in advance!

Luca Kiebel
  • 9,790
  • 7
  • 29
  • 44
King Leonidas
  • 23
  • 1
  • 8

2 Answers2

4

Shorten it regardless and only return the new number if it has the same value as the original.

function shortenOrReturn(num) {
  var shortNum = num.toFixed(2);
  return (shortNum == num) ? shortNum : num;
}

console.log(shortenOrReturn(2.9000)) // becomes 2.90
console.log(shortenOrReturn(0.0001)) // remains 0.0001
console.log(shortenOrReturn(2.8540)) // becomes 2.854
console.log(shortenOrReturn(1.0100)) // becomes 1.01
console.log(shortenOrReturn(3.0000)) // becomes 3.00
Luca Kiebel
  • 9,790
  • 7
  • 29
  • 44
Royal Wares
  • 1,192
  • 10
  • 23
  • You beat me by few minutes. – Rajesh Sep 06 '18 at 13:54
  • This seems to work like a charm ! I don't get how it only returns up until 3 decimal points though? If the original number has 4 decimal points.. I.e. 3.0010 would return 3.001 however, the input was 3.0010 ? – King Leonidas Sep 06 '18 at 13:59
  • JavaScript does that on the original number itself, if you were working with strings you could keep the extra 0 at the end, but when it's a number it's just extra junk that JS deals with. Type 3.0010 in the JS console and watch it throw back 3.001. – Royal Wares Sep 06 '18 at 14:02
  • Ah so my `num.toString()` conversion was actually working against me.. Interesting stuff. Javascript is weird.. . – King Leonidas Sep 06 '18 at 14:04
  • Yes, I would recommend looking into primitive data types in programming to give you a really strong grasp. I'm pretty sure if JavaScript had a slogan it would be "Javascript - it's weird". – Royal Wares Sep 06 '18 at 14:06
  • Thanks, I used to know about the primitive data types, but JS primitives behave nothing like actual primitive data types though :P Thanks for the help! – King Leonidas Sep 06 '18 at 14:10
-1

Here my suggestion:

function removeZerosFromRight (input) {
      return Number(input.toString())
    }

example:

  • removeZerosFromRight(123.0011) => 123.0011
  • removeZerosFromRight(123.0010) => 123.001
  • removeZerosFromRight(123.0100) => 123.01
  • removeZerosFromRight(123.1000) => 123.1
  • removeZerosFromRight(123.02) => 123.02
  • This function doesn't actually do anything assuming the input is a number, you could immediately return the input and achieve the same thing. The removal of the 0 is due to how JS stores numbers, not the type casting. Have a look at the comments from my answer for more details. – Royal Wares Sep 06 '18 at 14:11
  • This fails the very first test case that 2.9000 should become 2.90 – Wyck Sep 06 '18 at 14:49