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

Issue

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

Solution

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))

Answered By – brk

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published