Javascript 如何从浮点数中获取分数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4667238/
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-23 13:32:46  来源:igfitidea点击:

How to get a fraction from a float number?

javascriptjquery

提问by Richard Knop

I have a floating point number:

我有一个浮点数:

var f = 0.1457;

Or:

或者:

var f = 4.7005

How do I get just the fraction remainder as integer?

如何将分数余数作为整数?

I.e. in the first example I want to get:

即在我想得到的第一个例子中:

var remainder = 1457;

In the second example:

在第二个例子中:

var remainder = 7005;

采纳答案by Jimmy Chandra

This will do it (up to the 4 digits that you want, change the multipler (10000) to larger or smaller if you want smaller or larger number):

这将做到这一点(最多为您想要的 4 位数字,如果您想要更小或更大的数字,请将倍数(10000)更改为更大或更小):

Math.ceil(((f < 1.0) ? f : (f % Math.floor(f))) * 10000)

回答by Martina

function frac(f) {
    return f % 1;
}

hope that helps ;-)

希望有帮助;-)

回答by metalim

While this is not what most people will want, but TS asked for fract as integer, here it is:

虽然这不是大多数人想要的,但 TS 要求 fract 作为整数,这里是:

function fract(n){ return Number(String(n).split('.')[1] || 0); }
fract(1.23) // = 23
fract(123) // = 0
fract(0.0008) // = 8

回答by lms

You can subtract the floor of the number, giving you just the fractional part, and then multiply by 10000, i.e.:

您可以减去数字的下限,只得到小数部分,然后乘以 10000,即:

var remainder = (f-Math.floor(f))*10000;

回答by JJB1980

parseInt(parseFloat(amount).toString().split('.')[1], 10)

parseInt(parseFloat(amount).toString().split('.')[1], 10)

回答by miha

This also depends on what you want to do with the remainder (as commenters already asked). For instance, if the base number is 1.03, do you want the returned remainder as 3 or 03 -- I mean, do you want it as a number or as a string (for purposes of displaying it to the user). One example would be article price display, where you don't want to conver 03 to 3 (for instance $1.03) where you want to superscript 03.

这也取决于您想对其余部分做什么(正如评论者已经问过的那样)。例如,如果基数是 1.03,您希望返回的余数是 3 还是 03 - 我的意思是,您希望它是数字还是字符串(为了向用户显示)。一个例子是文章价格显示,您不想将 03 转换为 3(例如 1.03 美元),而您想要在 03 上标。

Next, the problem is with float precision. Consider this:

接下来,问题在于浮点精度。考虑一下:

var price = 1.03;
var frac = (price - Math.floor(price))*100;
// frac = 3.0000000000000027

So you can "solve" this by slicing the string representation without multiplication (and optional zero-padding) in such cases. At the same time, you avoid floating precision issue. Also demonstrated in this jsfiddle.

因此,在这种情况下,您可以通过在没有乘法(和可选的零填充)的情况下对字符串表示进行切片来“解决”这个问题。同时,您可以避免浮动精度问题。也在这个 jsfiddle 中进行了演示。

This postabout floating precision might help as well as this one.

这篇文章有关浮动精度可能会帮助以及这一个

回答by sce8cbm

I would argue that, assuming we want to display these values to the user, treating these numbers as strings would be the best approach. This gets round the issue of fractional values such as 0.002.

我认为,假设我们想向用户显示这些值,将这些数字视为字符串将是最好的方法。这解决了诸如 0.002 之类的小数值问题。

I came accross this issue when trying to display prices with the cents in superscript.

我在尝试用上标美分显示价格时遇到了这个问题。

let price = 23.43; // 23.43
let strPrice = price.toFixed(2) + ''; // "23.43"
let integer = strPrice.split(".")[0] // "23"
let fractional = strPrice.split(".")[1] // "43"

回答by svetoslav80

var strNumber = f.toString();
var remainder = strNumber.substr(strNumber.indexOf('.') + 1, 4);
remainder = Number(reminder);