1

I am displaying dimensions for ad to be published in newspaper, the height / width seems to use millimetres with incredibly small decimals like x.0002mm, I want numbers to be rounded down (always down, never up) to the nearest whole mm?
For example,
Input -> height: 380.0002mm width: 262.0002mm
expected Output-> height: 380mm width: 262mm

I already tried .toFixed(), but it is not working for large decimal inputs
For example,
sample Input -> height: 380.1252mm width: 262.6592mm
expected Output-> height: 380.1252mm width: 262.6592mm

I want number to be round down only if decimal number is very small for ex: 380.0002, I want to avoid rounding down if decimal number is greater, for ex: 380.1252

Adil Khan
  • 21
  • 5
  • 2
    Use `Math.floor()` – Walf Feb 10 '17 at 07:34
  • Possible duplicate of [How can I round down a number in Javascript?](http://stackoverflow.com/questions/1435975/how-can-i-round-down-a-number-in-javascript) – Walf Feb 10 '17 at 07:37
  • @Walf I want number to be round down only if decimal number is very small `for ex: 380.0002`, I want to avoid rounding down if decimal number is greater, `for ex: 380.1252` – Adil Khan Feb 10 '17 at 09:25
  • The chosen solution is hideous. `var flooredNum = Math.floor(num); if (num - flooredNum <= 0.0002) num = flooredNum;` – Walf Mar 14 '17 at 04:28

2 Answers2

2

Check this link, Formatting numbers for decimals

var number = 380.0002
number.toPrecision(6) //returns 380.000 (padding)
number.toPrecision(4) //returns 380.0 (round up)
number.toPrecision(3) //returns 380
Math.floor(380.0002) //returns 380
Janatbek Orozaly
  • 445
  • 3
  • 17
  • Thanks for quick response, actually I want to round number only if decimal is very small. "toPrecision()" helped me for that for ex: `var number = 380.0002; number.toPrecision(3);//OUTPUT=380 ` but I don't want to round down if decimal is big for ex: 380.1252. `var number = 380.1252; number.toPrecision(3);//OUTPUT=380 //But my expected output for this is 380.1252 only. ` – Adil Khan Feb 10 '17 at 09:20
  • I added new response – Janatbek Orozaly Feb 10 '17 at 09:44
0

This should work for you

function decimalPlaces(num) {
 var match = (''+num).match(/(?:\.(\d+))?(?:[eE]([+-]?\d+))?$/);
 if (!match) { return 0; }
 return Math.max(
   0,
   // Number of digits right of decimal point.
   (match[1] ? match[1].length : 0)
   // Adjust for scientific notation.
   - (match[2] ? +match[2] : 0));
 }

var length = (num + '').replace('.', '').length;  // for floats
console.log(length);
if (num.toPrecision(length).includes(".00")){
num = Math.floor(num);
console.log(num)
}else{

 var dec = decimalPlaces(num);
 num = num.toFixed(dec);
 console.log(num)
}

And BTW Credit for decimalPlaces() function: Javascript: How to retrieve the number of decimals of a string number?

Community
  • 1
  • 1
Janatbek Orozaly
  • 445
  • 3
  • 17
  • Found 1 issue, It is not working when I pass `var num = 380.999` ,output is coming as `380` – Adil Khan Feb 10 '17 at 10:13
  • I have edited your answer , Reason: `var num = 380.99999; num.toFixed(3)` is returning `380.000` so I have added addtional condition **`&& !(num.toFixed(dec).includes(".99"))`** to bypass it. Please suggest if you have any better way to do it. – Adil Khan Feb 13 '17 at 04:19