1

I need to change 8.34 to 8.4, 27.62 to 27.7 (not 27.6) i came with code so far

var a = 3.34
a.toFixed(1) // 8.3 the result i need is 8.4
var b = 27.62 // 2.7 this correct

tried using several function like round() or Math.floor(), i know the result should be 8.34 if we round it 8.3 because it's not 8.35, but can we make 8.34 become to 8.4 ?

Khairu Aqsara
  • 1,321
  • 3
  • 14
  • 27
  • Or: [Round to at most 2 decimal places (only if necessary)](https://stackoverflow.com/questions/11832914/round-to-at-most-2-decimal-places-only-if-necessary) – Andreas Dec 01 '18 at 09:57

2 Answers2

3

Multiply by 10, use Math.ceil, and then divide by 10:

const convert = num => console.log(Math.ceil(num * 10) / 10);
convert(8.34);
convert(27.62);
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
2

Use Math.ceil

let a = 8.34;
let b = 27.62;

console.log(Math.ceil(a*10)/10)
console.log(Math.ceil(b*10)/10)
Nikhil Aggarwal
  • 28,197
  • 4
  • 43
  • 59