# How to print decimal numbers only when the digit after decimal point is numbers except 0?

**URL:** <https://forum.freecodecamp.org/t/how-to-print-decimal-numbers-only-when-the-digit-after-decimal-point-is-numbers-except-0/321429>\
**Category:** General\
**Created:** [October 13, 2019, 8:30am UTC](https://forum.freecodecamp.org/t/how-to-print-decimal-numbers-only-when-the-digit-after-decimal-point-is-numbers-except-0/321429 "2019-10-13T08:30:18Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![anasansari157](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/anasansari157/32/123775_2.png) [@anasansari157](https://forum.freecodecamp.org/u/anasansari157)\
**Post date:** [October 13, 2019, 8:30am UTC](https://forum.freecodecamp.org/t/how-to-print-decimal-numbers-only-when-the-digit-after-decimal-point-is-numbers-except-0/321429/1 "2019-10-13T08:30:18Z")

</div>

I want to make a function that prints accurate result(including the numbers after decimal) when the right value after the decimal is anything but 0.

For example,

> if num = 5.9999947575, return 5.9999947575  
> if num 50000.0000, return 50000  
> if num = 500.0000023, retutn 500.0000023  
> if num 7215.22152790009999, return 7215.22152790009999  
> if num = 1000.0000 return 1000

Please help me!

---

<div class="post-metadata">

**Author:** ![Sky020](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/sky020/32/128093_2.png) [@Sky020](https://forum.freecodecamp.org/u/Sky020)\
**Post date:** [October 13, 2019, 8:50am UTC](https://forum.freecodecamp.org/t/how-to-print-decimal-numbers-only-when-the-digit-after-decimal-point-is-numbers-except-0/321429/2 "2019-10-13T08:50:53Z")

</div>

There might be a much simpler way, but I would write a function that turns the number into a string, loop through each _character_, if it is after the decimal point, and not a zero, round it off.

---

<div class="post-metadata">

**Author:** ![anasansari157](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/anasansari157/32/123775_2.png) [@anasansari157](https://forum.freecodecamp.org/u/anasansari157)\
**Post date:** [October 13, 2019, 8:56am UTC](https://forum.freecodecamp.org/t/how-to-print-decimal-numbers-only-when-the-digit-after-decimal-point-is-numbers-except-0/321429/3 "2019-10-13T08:56:41Z")

</div>

I also read read somewhere that you can split the number in two parts one before the decimal and one after the decimal. Then you can check the right values of the decimals for the zeros. But its not working

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [October 13, 2019, 10:47am UTC](https://forum.freecodecamp.org/t/how-to-print-decimal-numbers-only-when-the-digit-after-decimal-point-is-numbers-except-0/321429/4 "2019-10-13T10:47:03Z")

</div>

show your code - so we can see what’s not working

---

<div class="post-metadata">

**Author:** ![DanCouper](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/dancouper/32/158608_2.png) [@DanCouper](https://forum.freecodecamp.org/u/DanCouper)\
**Post date:** [October 13, 2019, 10:47am UTC](https://forum.freecodecamp.org/t/how-to-print-decimal-numbers-only-when-the-digit-after-decimal-point-is-numbers-except-0/321429/5 "2019-10-13T10:47:37Z")

</div>

If this is JS, it will do that anyway, can you explain what issue you’re having in a bit more detail?

```auto
> 50000.0000
50000
> 5.9999947575
5.9999947575
> 500.0000023
500.0000023

```

That’s JavaScript, just pasting the numbers into the console.

---

<div class="post-metadata">

**Author:** ![anasansari157](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/anasansari157/32/123775_2.png) [@anasansari157](https://forum.freecodecamp.org/u/anasansari157)\
**Post date:** [October 13, 2019, 11:29am UTC](https://forum.freecodecamp.org/t/how-to-print-decimal-numbers-only-when-the-digit-after-decimal-point-is-numbers-except-0/321429/6 "2019-10-13T11:29:05Z")

</div>

[http://www.codewars.com/kata/57e93e4a2aee4974d4000c2f/train/javascript](http://www.codewars.com/kata/57e93e4a2aee4974d4000c2f/train/javascript)

This is the link to the problem I wanted to solve.

This is my code:

```auto
function vestBuy(price, haggle){
  switch (haggle) {
    case 'light':
      price -= 0.2 * price;
      return price;
      break;
      
    case 'medium':
      price -= 0.3 * price;
      return price;
      break;
      
    case 'heavy':
      price -= 0.4 * price;
      return price;
      break;
      
    case 'walkandswear':
      price -= 0.9 * price;
      return price;
      break;
      
    default:
      return 'Run!!';
    }
}

```

The problem is when the returned price (answer) is in natural numbers, it works fine.  
But when the returned price is a float (say 502.1489), the output gets rounded off (it becomes 502).  
I want the output price to be what it is when it is in float( exact 502.1489). But if the output is (502.0000) I want it to be 502.  
In simple terms, only shorten it when the decimal values are 0 else keep the way it is.

Please let me know if you guys understand me. I will explain my problem again if you want.

---

<div class="post-metadata">

**Author:** ![anasansari157](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/anasansari157/32/123775_2.png) [@anasansari157](https://forum.freecodecamp.org/u/anasansari157)\
**Post date:** [October 13, 2019, 11:34am UTC](https://forum.freecodecamp.org/t/how-to-print-decimal-numbers-only-when-the-digit-after-decimal-point-is-numbers-except-0/321429/7 "2019-10-13T11:34:21Z")

</div>

Yeah, sure. The problem is when the returned price (answer) is in natural numbers, it works fine.  
But when the returned price is a float (say 502.1489), the output gets rounded off (it becomes 502).  
I want the output price to be what it is when it is in float( exact 502.1489). But if the output is (502.0000) I want it to be 502.  
In simple terms, only shorten it when the decimal values are 0 else keep the way it is.

I also uploaded my code below.

---

<div class="post-metadata">

**Author:** ![DanCouper](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/dancouper/32/158608_2.png) [@DanCouper](https://forum.freecodecamp.org/u/DanCouper)\
**Post date:** [October 13, 2019, 12:21pm UTC](https://forum.freecodecamp.org/t/how-to-print-decimal-numbers-only-when-the-digit-after-decimal-point-is-numbers-except-0/321429/8 "2019-10-13T12:21:36Z")

</div>

Is there more code than this? Because nothing there is rounding anything - how is 502.1489 coming out as 502? Also, surely 5.9999947575 can’t be a valid answer, as it specifies that the answers have to be in Rupia?

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 16, 2021, 12:58pm UTC](https://forum.freecodecamp.org/t/how-to-print-decimal-numbers-only-when-the-digit-after-decimal-point-is-numbers-except-0/321429/9 "2021-01-16T12:58:33Z")

</div>


