在 PHP 中获取浮点数小数部分的最佳方法是什么?

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

What's the best way to get the fractional part of a float in PHP?

php

提问by tkrehbiel

Simple one: How would you find the fractional part of a floating point number in PHP? For example, if I have the value 1.25, I want to return 0.25.

简单一:你如何在 PHP 中找到浮点数的小数部分?例如,如果我的值为 1.25,我想返回 0.25。

回答by nlucaroni

$x = $x - floor($x)

回答by Alan Storm

Don't forget that you can't trust floating point arithmetic to be 100% accurate. If you're concerned about this, you'll want to look into the BCMath Arbitrary Precision Mathematicsfunctions.

不要忘记,您不能相信浮点运算是 100% 准确的。如果您对此感到担心,您将需要研究BCMath 任意精度数学函数。

$x = 22.732423423423432;
$x = bcsub(abs($x),floor(abs($x)),20);

You could also hack on the string yourself

你也可以自己破解字符串

$x = 22.732423423423432;    
$x = strstr ( $x, '.' );

回答by sanmai

$x = fmod($x, 1);

Here's a demo:

这是一个演示:

<?php
$x = 25.3333;
$x = fmod($x, 1);
var_dump($x);

Should ouptut

应该输出

double(0.3333)

Credit.

信用。

回答by Jeremy Ruten

If if the number is negative, you'll have to do this:

如果数字为负数,则必须执行以下操作:

 $x = abs($x) - floor(abs($x));

回答by Michael Fenwick

The answer provided by nlucaroni will only work for positive numbers. A possible solution that works for both positive as well as negative numbers is:

nlucaroni 提供的答案仅适用于正数。适用于正数和负数的可能解决方案是:

$x = $x - intval($x)

回答by Ethan Gunderson

My PHP skills are lacking but you could minus the result of a floor from the original number

我的 PHP 技能不足,但您可以从原始数字中减去一个楼层的结果

回答by Ethan Gunderson

However, if you are dealing with something like perlin noise or another graphical representation, the solution which was accepted is correct. It will give you the fractional part from the lower number.

但是,如果您正在处理类似柏林噪声或其他图形表示的问题,则接受的解决方案是正确的。它将为您提供较低数字的小数部分。

i.e:

IE:

  • .25: 0 is integer below, fractional part is .25
  • -.25: -1 is integer below, fractional part is .75
  • .25: 0 以下为整数,小数部分为 0.25
  • -.25: -1 是下面的整数,小数部分是 0.75

With the other solutions, you will repeat 0 as integer below, and worse, you will get reversed fractional values for all negative numbers.

对于其他解决方案,您将在下面将 0 作为整数重复,更糟糕的是,您将获得所有负数的反向小数值。

回答by Paul M

Some of the preceding answers are partial. This, I believe, is what you need to handle allsituations:

前面的一些答案是片面的。我相信,这是您处理所有情况所需要的:

function getDecimalPart($floatNum) {
    return abs($floatNum - intval($floatNum));
}

$decimalPart = getDecimalPart($floatNum);

回答by dmvslv

You can use fmodfunction:

您可以使用fmod功能:

$y = fmod($x, 1); //$x = 1.25 $y = 0.25

回答by SirDagen

To stop the confusion on this page actually this is the best answer, which is fast and works for both positive and negative values of $x:

为了停止这个页面上的混乱,实际上这是最好的答案,它很快并且适用于 $x 的正值和负值:

$frac=($x<0) ? $x-ceil($x) : $x-floor($x);

I ran speed tests of 10 million computations on PHP 7.2.15 and even though both solutions give the same results, fmod is slower than floor/ceil.

我在 PHP 7.2.15 上运行了 1000 万次计算的速度测试,尽管两种解决方案都给出了相同的结果,但 fmod 比 floor/ceil 慢。

$frac=($x<0) ? $x-ceil($x) : $x-floor($x);-> 490-510 ms (depending on the sign of $x)

$frac=($x<0) ? $x-ceil($x) : $x-floor($x);-> 490-510 毫秒(取决于 $x 的符号)

$frac=fmod($x, 1);-> 590 - 1000 ms (depending on the value of $x)

$frac=fmod($x, 1);-> 590 - 1000 毫秒(取决于 $x 的值)

Whereas the actual empty loop itself takes 80 ms (which is included in above timings).

而实际的空循环本身需要 80 毫秒(包括在上述时间中)。

Test script:

测试脚本:

$x=sqrt(2)-0.41421356237;

$time_start = microtime(true);
for ($i=0;$i<=9999999;$i++) {
    //$frac=fmod($x, 1); // version a
    $frac=($x<0) ? $x-ceil($x) : $x-floor($x); // version b
}
$time_end = microtime(true);

$time = $time_end - $time_start;