简体   繁体   English

从数字中删除小数点并将其转换为PHP中的整数

[英]Removing a decimal point from a number and converting it to a whole number in PHP

I have a small issue.... 我有一个小问题。

I have a php function which is generating a number based on dividing two whole numbers. 我有一个php函数,它基于两个整数相除生成一个数字。 This will always be expressed as a percentage of 1. 始终以1的百分比表示。

eg, my function would generate a number such as 0.3 or 0.01222 or 0.23 例如,我的函数将生成一个数字,例如0.3或0.01222或0.23

I need to convert this percentage to a whole number number as a part of 100 with three digits. 我需要将此百分比转换为一个整数,作为三位数的100的一部分。

so 0.3 would become 030, 0.01222 would become 001 and 1 would become 100 因此0.3变为030,0.01222变为001,1变为100

Is there a simple way to do this in PHP? 有没有一种简单的方法可以在PHP中执行此操作?

Cheers :) 干杯:)

The end-result will no longer be a number but more a string, so just go with string operations: 最终结果将不再是数字,而是更多的字符串,因此只需进行字符串操作即可:

$number = 0.3;

// Multiply by 100 and remove stuff after decimal point
$string = (string) floor($number * 100);

// Add zeros until the string has a total length of three characters
$string = str_pad($string, 3, '0', STR_PAD_LEFT);

Yes, there is: 就在这里:

$num = 0.01222;
$num2 = ceil($num * 100);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM