Skip to content
Advertisement

How to format a number to Lac or Crores if it has minus sign before it

I have used a function to convert the numbers to lakhs and crores which works fine for numbers which doesn’t have minus sign before but if the number has minus sign it doesn’t get changed.I have tried to convert the number into string and replaced the string and then made it to check but it didn’t works.

function numDifferentiation (val) {
  if (val >= 10000000) {
    val = (val / 10000000).toFixed(2) + ' Cr';
  } else if (val >= 100000) {
    val = (val / 100000).toFixed(2) + ' Lac';
  }
  /*else if(val >= 1000) val = (val/1000).toFixed(2) + ' K';*/
  return val;
}

I need the output even if it has minus sign before like -330000 as -3.30 Lac

Advertisement

Answer

You can use Math.abs().

This function returns the absolute value of a number

function numDifferentiation(value) {
  var val = Math.abs(value)
  if (val >= 10000000) {
    val = (val / 10000000).toFixed(2) + ' Cr';
  } else if (val >= 100000) {
    val = (val / 100000).toFixed(2) + ' Lac';
  }
  return val;
}
console.log(numDifferentiation(-50000000))
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement