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。
8 个解决方案
#1
66
$x = $x - floor($x)
#2
13
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 Mathematics functions.
不要忘记你不能相信浮点运算是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, '.' );
#3
9
If if the number is negative, you'll have to do this:
如果数字为负数,则必须执行以下操作:
$x = abs($x) - floor(abs($x));
#4
8
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)
#5
7
$x = fmod($x, 1);
Here's a demo:
这是一个演示:
<?php
$x = 25.3333;
$x = fmod($x, 1);
var_dump($x);
Should ouptut
应该ouptut
double(0.3333)
信用。
#6
3
My PHP skills are lacking but you could minus the result of a floor from the original number
我的PHP技能缺乏,但你可以从原始数字减去一个楼层的结果
#7
1
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.
但是,如果您正在处理类似于perlin噪声或其他图形表示的内容,则接受的解决方案是正确的。它会从较低的数字中给出小数部分。
i.e:
即:
-
.25
: 0 is integer below, fractional part is .25 - .25:0是整数,小数部分是.25
-
-.25
: -1 is integer below, fractional part is .75 - -25:-1是下面的整数,小数部分是.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作为整数,更糟糕的是,您将获得所有负数的反转小数值。
#8
1
Some of the preceding answers are partial. This, I believe, is what you need to handle all situations:
前面的一些答案是部分的。我相信,这是你处理所有情况所需要的:
function getDecimalPart($floatNum) {
return abs($floatNum - intval($floatNum));
}
$decimalPart = getDecimalPart($floatNum);
#1
66
$x = $x - floor($x)
#2
13
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 Mathematics functions.
不要忘记你不能相信浮点运算是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, '.' );
#3
9
If if the number is negative, you'll have to do this:
如果数字为负数,则必须执行以下操作:
$x = abs($x) - floor(abs($x));
#4
8
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)
#5
7
$x = fmod($x, 1);
Here's a demo:
这是一个演示:
<?php
$x = 25.3333;
$x = fmod($x, 1);
var_dump($x);
Should ouptut
应该ouptut
double(0.3333)
信用。
#6
3
My PHP skills are lacking but you could minus the result of a floor from the original number
我的PHP技能缺乏,但你可以从原始数字减去一个楼层的结果
#7
1
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.
但是,如果您正在处理类似于perlin噪声或其他图形表示的内容,则接受的解决方案是正确的。它会从较低的数字中给出小数部分。
i.e:
即:
-
.25
: 0 is integer below, fractional part is .25 - .25:0是整数,小数部分是.25
-
-.25
: -1 is integer below, fractional part is .75 - -25:-1是下面的整数,小数部分是.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作为整数,更糟糕的是,您将获得所有负数的反转小数值。
#8
1
Some of the preceding answers are partial. This, I believe, is what you need to handle all situations:
前面的一些答案是部分的。我相信,这是你处理所有情况所需要的:
function getDecimalPart($floatNum) {
return abs($floatNum - intval($floatNum));
}
$decimalPart = getDecimalPart($floatNum);