如何获取一个数的整数部分和小数部分?
比如说,给定 1.25 - 我如何获得这个数字的“1”和“25”部分?
我需要检查小数部分是 .0、.25、.5 还是 .75。
Given, say, 1.25 - how do I get "1" and ."25" parts of this number?
I need to check if the decimal part is .0, .25, .5, or .75.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(20)
然后与 1/4、1/2、3/4 等进行比较。
如果是负数,请使用以下命令:
$returnUnsigned
会阻止其生成 -1.25到 -1 & -0.25Then compare against 1/4, 1/2, 3/4, etc.
In cases of negative numbers, use this:
The
$returnUnsigned
stops it from making -1.25 in to -1 & -0.25此代码将为您拆分它:
其中 $whole 是整数,$decimal 将包含小数点后的数字。
This code will split it up for you:
where $whole is the whole number and $decimal will have the digits after the decimal point.
Floor() 方法不适用于负数。这每次都有效:
...也适用于底片(相同的代码,不同的数字):
The floor() method doesn't work for negative numbers. This works every time:
...also works for negatives (same code, different number):
只是为了与众不同:)
CodePad。
作为一个额外的好处,它只会在双方都包含数字的情况下进行拆分。
Just to be different :)
CodePad.
As an added benefit, it will only split where both sides consist of digits.
一个简短的方法(使用下限和 fmod)
然后将 $decimal 与 0、.25、.5 或 .75 进行比较
a short way (use floor and fmod)
then compare $decimal to 0, .25, .5, or .75
将其转换为 int 并减去
或者只是为了得到小数以进行比较
Cast it as an int and subtract
Or just to get the decimal for comparison
对于那些想要将整数部分和小数部分拆分为两个整数分隔值的人来说,这只是一个新的简单解决方案:
5.25 ->整数部分:5;小数部分:25
这种方式不涉及基于字符串的函数,并且可以防止其他数学运算中可能出现的准确性问题(例如使用 0.49999999999999 而不是 0.5)。
还没有对极值进行彻底测试,但它对我来说对于价格计算来说效果很好。
但是,要小心!现在从-5.25你得到:整数部分:-5;小数部分:-25
如果您想始终获得正数,只需在计算前添加
abs()
:最后,打印带有 2 位小数的价格的奖励片段:
。 ..这样你就可以避免得到 5.5 而不是 5.05。 ;)
Just a new simple solution, for those of you who want to get the Integer part and Decimal part splitted as two integer separated values:
5.25 -> Int part: 5; Decimal part: 25
This way is not involving string based functions, and is preventing accuracy problems which may arise in other math operations (such as having 0.49999999999999 instead of 0.5).
Haven't tested thoroughly with extreme values, but it works fine for me for price calculations.
But, watch out! Now from -5.25 you get: Integer part: -5; Decimal part: -25
In case you want to get always positive numbers, simply add
abs()
before the calculations:Finally, bonus snippet for printing prices with 2 decimals:
...so that you avoid getting 5.5 instead of 5.05. ;)
还有一个 fmod 函数,可以使用:
fmod($my_var, 1)
将返回相同的结果,但有时会出现较小的舍入误差。
There's a fmod function too, that can be used :
fmod($my_var, 1)
will return the same result, but sometime with a small round error.
PHP 5.4+
PHP 5.4+
这是我使用的方式:
This is the way which I use:
Brad Christie 的方法本质上是正确的,但可以写得更简洁。
这与他的方法相同,但更短,因此更容易理解。
Brad Christie's method is essentially correct but it can be written more concisely.
This is equivalent to his method but shorter and hopefully easier to understand as a result.
我很难找到一种方法来实际区分美元金额和小数点后的金额。我想我大部分都弄清楚了,并且想分享是否有人遇到麻烦
所以基本上...
如果价格是1234.44...整体将是1234,小数将是44,或者
如果价格是1234.01...整体将是1234,小数点为 01,或者
如果价格为 1234.10...整体为 1234,小数点为 10
,依此类推
I was having a hard time finding a way to actually separate the dollar amount and the amount after the decimal. I think I figured it out mostly and thought to share if any of yall were having trouble
So basically...
if price is 1234.44... whole would be 1234 and decimal would be 44 or
if price is 1234.01... whole would be 1234 and decimal would be 01 or
if price is 1234.10... whole would be 1234 and decimal would be 10
and so forth
如果你可以指望它总是有 2 个小数位,你可以只使用字符串操作:
不知道性能,但对于我的简单情况来说,这要好得多......
If you can count on it always having 2 decimal places, you can just use a string operation:
No idea of performance but for my simple case this was much better...
为了防止额外的浮点小数(即 50.85 - 50 给出 0.850000000852),在我的例子中,我只需要 2 位小数来表示钱分。
To prevent the extra float decimal (i.e. 50.85 - 50 give 0.850000000852), in my case I just need 2 decimals for money cents.
试试这个方法...这样更容易
Try it this way... it's easier like this
你也可以使用这样的东西:
You could also use something like this:
如果您希望对两半进行显式类型转换,那么 sscanf() 是一个很好的调用。
代码:(Demo)
输出:
或者您可以单独分配两个变量:
将小数部分转换为浮点数是例如,当将小时的十进制表达式转换为小时和分钟时特别有用。 (演示)
输出:
If you want the two halves to be explicitly type cast, then
sscanf()
is a great call.Code: (Demo)
Output:
Or you can assign the two variables individually:
Casting the decimal portion as a float is particularly useful when, say, converting decimal expression of hours to hours and minutes. (Demo)
Output:
这里没有看到简单的模数...
在这种情况下,获取
$wholeAs?
和$decimal
并不依赖于另一个。 (您可以独立获取 3 个输出中的 1 个。)我显示了$wholeAsFloat
和$wholeAsInt
因为floor()
返回一个 float 类型数字,即使它返回的数字始终是整数。 (如果您将结果传递给类型提示的函数参数,这一点很重要。)我希望将浮点数的小时/分钟(例如 96.25)分别拆分为小时和分钟,以用于 DateInterval 实例为 96 小时 15 分钟。我这样做如下:
在我的情况下,我不关心秒数。
Not seen a simple modulus here...
In this case getting both
$wholeAs?
and$decimal
don't depend on the other. (You can just take 1 of the 3 outputs independently.) I've shown$wholeAsFloat
and$wholeAsInt
becausefloor()
returns a float type number even though the number it returns will always be whole. (This is important if you're passing the result into a type-hinted function parameter.)I wanted this to split a floating point number of hours/minutes, e.g. 96.25, into hours and minutes separately for a DateInterval instance as 96 hours 15 minutes. I did this as follows:
I didn't care about seconds in my case.