如果数字前面有减号,则如何将其格式化为Lac或Crores [英] How to format a number to Lac or Crores if it has minus sign before it

查看:93
本文介绍了如果数字前面有减号,则如何将其格式化为Lac或Crores的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用一个函数将数字转换为十万美元和千千万万美元以前没有负号的数字,但是如果它有负号,它不会被更改.我试图将数字转换为字符串并替换字符串,然后进行检查,但没有用./p>

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;
}

即使像-330000这样的-3.30 Lac,我也需要输出,即使它带有负号

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

推荐答案

您可以使用

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

这篇关于如果数字前面有减号,则如何将其格式化为Lac或Crores的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆