How do I split a number by the decimal point in php?
在php中,如何将一个数字除以小数点?
I've got $num = 15/4; which turns $num into 3.75. I would like to split out the 3 and the 75 parts, so $int = 3 and $dec = 75. My non-working code is:
我有$num = 15/4;这将使$num变为3.75。我想把3和75部分分开,所以$int = 3和$dec = 75。我的非工作代码是:
$num = 15/4; // or $num = 3.75;
list($int, $dec) = split('.', $num);
but that results in empty $int and $dec.
但这会导致空的$int和$dec。
Thanks in advance.
提前谢谢。
8 个解决方案
#1
20
If you explode
the decimal representation of the number, you lose precision. If you don't mind, so be it (that's ok for textual representation). Take the locale into account! We Belgians use a comma (at least the non-programming ones :).
如果把数字的十进制表示搞砸,就会失去精度。如果你不介意的话,那就去吧(这对文本表示来说是可以的)。考虑到语言环境!我们比利时人使用逗号(至少是非编程的逗号:)。
If you do mind (for computations e.g.), you can use the floor
function:
如果你不介意的话,你可以用楼层函数:
$num = 15/4
$intpart = floor( $num ) // results in 3
$fraction = $num - $intpart // results in 0.75
Note: this is for positive numbers. For negative numbers you can invert the sign, use the positive approach, and reinvert the sign of the int part.
注意:这是正数。对于负数,您可以对符号进行逆变,使用正向方法,并重新插入int部分的符号。
#2
#3
6
$num = 15/4; // or $num = 3.75;
list($int, $dec) = explode('.', $num);
#4
4
$int = $num > 0 ? floor($num) : ceil($num);
$dec = $num - $int;
If you want $dec
to be positive when $num
is negative (like the other answers) you could do:
如果您希望$num为负数时$dec为正数(如其他答案),您可以这样做:
$dec = abs($num - $int);
#5
2
$num = 3.75;
$fraction = $num - (int) $num;
#6
1
$num = 15/4;
substr(strrchr($num, "."), 1)
#7
0
In case when you don't want to lose precision, you can use these:
如果您不想失去精度,您可以使用以下方法:
$number = 10.10;
$number = number_format($number, 2, ".", ",");
sscanf($number, '%d.%d', $whole, $fraction);
// you will get $whole = 10, $fraction = 10
#8
0
This works for positive AND negative numbers:
这适用于正数和负数:
$num = 5.7;
$whole = (int) $num; // 5
$frac = $num - (int) $num; // .7
#1
20
If you explode
the decimal representation of the number, you lose precision. If you don't mind, so be it (that's ok for textual representation). Take the locale into account! We Belgians use a comma (at least the non-programming ones :).
如果把数字的十进制表示搞砸,就会失去精度。如果你不介意的话,那就去吧(这对文本表示来说是可以的)。考虑到语言环境!我们比利时人使用逗号(至少是非编程的逗号:)。
If you do mind (for computations e.g.), you can use the floor
function:
如果你不介意的话,你可以用楼层函数:
$num = 15/4
$intpart = floor( $num ) // results in 3
$fraction = $num - $intpart // results in 0.75
Note: this is for positive numbers. For negative numbers you can invert the sign, use the positive approach, and reinvert the sign of the int part.
注意:这是正数。对于负数,您可以对符号进行逆变,使用正向方法,并重新插入int部分的符号。
#2
7
Try explode
试着爆炸
list($int,$dec)=explode('.', $num);
as you don't really need to use a regex based split. Split wasn't working for you as a '.' character would need escaping to provide a literal match.
因为您并不需要使用基于regex的分割。斯普利特不是为你工作的。字符需要转义才能提供文字匹配。
#3
6
$num = 15/4; // or $num = 3.75;
list($int, $dec) = explode('.', $num);
#4
4
$int = $num > 0 ? floor($num) : ceil($num);
$dec = $num - $int;
If you want $dec
to be positive when $num
is negative (like the other answers) you could do:
如果您希望$num为负数时$dec为正数(如其他答案),您可以这样做:
$dec = abs($num - $int);
#5
2
$num = 3.75;
$fraction = $num - (int) $num;
#6
1
$num = 15/4;
substr(strrchr($num, "."), 1)
#7
0
In case when you don't want to lose precision, you can use these:
如果您不想失去精度,您可以使用以下方法:
$number = 10.10;
$number = number_format($number, 2, ".", ",");
sscanf($number, '%d.%d', $whole, $fraction);
// you will get $whole = 10, $fraction = 10
#8
0
This works for positive AND negative numbers:
这适用于正数和负数:
$num = 5.7;
$whole = (int) $num; // 5
$frac = $num - (int) $num; // .7