Javascript 是否有任何JS函数来查找小数点前后的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7881319/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 03:56:21  来源:igfitidea点击:

Is there ay JS function to find value before and after decimal point

javascriptjquery

提问by user987880

I am validating a decimal number using JavaScript. Am just using NaN

我正在使用 JavaScript 验证十进制数。我只是在使用NaN

var a = 12345.67 Is there any javascript function to get the count or the value itself before and after decimal point .

var a = 12345.67 是否有任何 javascript 函数来获取小数点前后的计数或值本身。

before()  should return 1234
after() should return 67

Please dont suggest a substring!

请不要建议子字符串!

回答by John Hartsock

var a = 12345.67;

alert(a.toString().split(".")[0]); ///before
alert(a.toString().split(".")[1]); ///after

Here is a simple fiddle http://jsfiddle.net/qWtSc/

这是一个简单的小提琴http://jsfiddle.net/qWtSc/



zzzzBov's suggestion is this

zzzzBov 的建议是这个

Number.prototype.before = function () {
  var value = parseInt(this.toString().split(".")[0], 10);//before
  return value ? value : 0;
}

Number.prototype.after = function () {
  var value = parseInt(this.toString().split(".")[1], 10);//after
  return value ? value : 0;
}

Usage

用法

alert(a.before()); ///before
alert(a.after()); ///after

回答by Alex Wayne

beforeis easy. It's just a round down operation.

before简单。这只是一个舍入操作。

var before = function(n) {
  return Math.floor(n);
};

afteris harder without string processing. I mean how would you handle after(Math.PI)? You can't hold a integer with an infinite number of digits after all.

after没有字符串处理更难。我的意思是你会怎么处理after(Math.PI)?毕竟你不能持有一个无限位数的整数。

But with some string processing it's easy, just know it won't be exact due to the wonders of floating point math.

但是通过一些字符串处理很容易,只要知道由于浮点数学的奇迹,它不会是精确的。

var after = function(n) {
  var fraction = n.toString().split('.')[1];
  return parseInt(fraction, 10);
};

回答by rlemon

Playing off of other answers... and you wanted a 'numeric' version.. still easiest to convert it to a string and work off the split function...

播放其他答案......你想要一个“数字”版本......仍然最容易将其转换为字符串并处理拆分功能......

function getNatural(num) {
    return parseFloat(num.toString().split(".")[0]);
}
function getDecimal(num) {
    return parseFloat(num.toString().split(".")[1]);
}
var a = 12345.67;

alert(getNatural(a)); ///before
alert(getDecimal(a)); ///after

http://jsfiddle.net/rlemon/qWtSc/1/

http://jsfiddle.net/rlemon/qWtSc/1/

回答by evilone

var decimalPlaces = 2;    
var num = 12345.673
var roundedDecimal = num.toFixed(decimalPlaces);

var intPart = Math.floor(roundedDecimal);
var fracPart = parseInt((roundedDecimal - intPart), 10);

//or

var fractPart = (roundedDecimal - intPart) * Math.pow(10, decimalPlaces);

回答by T.Todua

To find the count/length of characters after dot:

要查找点后的字符数/长度:

var a = 12345.67;
var after_dot = (a.toString().split(".")[1]).length;
var before_dot= (a.toString().split(".")[0]).length;

回答by MaxArt

Unfortunately there's no way to get the factional part in a reliable way using math functions, because pretty odd roundings often occur, depending on the Javascript engine used. The best thing to do is to convert it to a string, and then checking if the results is in decimal or scientific notation.

不幸的是,没有办法使用数学函数以可靠的方式获得派系部分,因为经常发生非常奇怪的四舍五入,这取决于所使用的 Javascript 引擎。最好的办法是将其转换为字符串,然后检查结果是十进制还是科学记数法。

Number.prototype.after = function() {
    var string = this.toString();
    var epos = string.indexOf("e");
    if (epos === -1) { // Decimal notation
        var i = string.indexOf(".");
        return i === -1 ? "" : n.substring(i + 1);
    }
    // Scientific notation
    var exp = string.substring(epos + 1) - 0; // this is actually faster
                                              // than parseInt in many browsers
    var mantix = n.string.substring(0, epos).replace(".", "");
    if (exp >= -1) return mantix.substring(exp + 1);
    for (; exp < -1; exp++) mantix = "0" + mantix;
    return mantix;
}