如何在PHP中转换小数和分数
我试着把数字乘以2,这些数字要么是整数,要么是分数。我通过WordPress上的ACF字段动态输入这些数字作为文本字段。但有些动态数不是分数,而是整数,比如3或1。我想做的是把数字加倍,这样3/4就是1/2。但是当我乘以分数,比如3/4,它会变成1.5。我使用了fubar代码使这些数字保持为分数,但随后我遇到了正确分数显示为小数的问题 当你在份量中点击6时,它应该是配方的两倍 下面是我的模板中的一段代码片段,用于处理这些数字(我的javascript只是根据服务大小在单击时隐藏/显示数字):如何在PHP中转换小数和分数,php,wordpress,fractions,Php,Wordpress,Fractions,我试着把数字乘以2,这些数字要么是整数,要么是分数。我通过WordPress上的ACF字段动态输入这些数字作为文本字段。但有些动态数不是分数,而是整数,比如3或1。我想做的是把数字加倍,这样3/4就是1/2。但是当我乘以分数,比如3/4,它会变成1.5。我使用了fubar代码使这些数字保持为分数,但随后我遇到了正确分数显示为小数的问题 当你在份量中点击6时,它应该是配方的两倍 下面是我的模板中的一段代码片段,用于处理这些数字(我的javascript只是根据服务大小在单击时隐藏/显示数字):
对于这些,最好是将值作为小数,但以某种方式使用jQuery/javascript将其转换为分数,仅用于前端
想法?我会非常感激的
让我知道
谢谢
编辑
对于那些通过WordPress处理ACF字段的人,这里有一个简单的解决方案。$number字段是一个数字ACF字段,因此首先使用小数
<!-- Create the function -->
function convert($number) {
<!-- pull in the number field and multiply it by 2 (since we are
doubling -->
$number = $number * 2;
<!-- Create and Array of all the possibilities of fractions (this
instance is cooking measurements -->
$fractions = array(
0625 => '1/16',
1875 => '3/16',
3125 => '5/16',
4375 => '7/16',
5625 => '9/16',
6575 => '11/16',
8125 => '13/16',
875 => '14/16',
9375 => '15/16',
125 => '1/8',
33 => '1/3',
25 => '1/4',
66 => '2/3',
5 => '1/2',
375 => '3/8',
625 => '5/8',
75 => '3/4'
);
<!-- explode the part after the decimal point and convert it to the
fraction above -->
$parts = explode('.', $number);
if(isset($parts[1])) {
echo $parts[0].' '.$fractions[$parts[1]];
} else {
echo $number;
}
}
函数转换($number){
$number=$number*2;
$sections=数组(
0625 => '1/16',
1875 => '3/16',
3125 => '5/16',
4375 => '7/16',
5625 => '9/16',
6575 => '11/16',
8125 => '13/16',
875 => '14/16',
9375 => '15/16',
125 => '1/8',
33 => '1/3',
25 => '1/4',
66 => '2/3',
5 => '1/2',
375 => '3/8',
625 => '5/8',
75 => '3/4'
);
$parts=分解('.',$number);
if(isset($parts[1])){
回显$parts[0].'.$sections[$parts[1]];
}否则{
echo$number;
}
}
然后只添加在WP模板上被拉到$To值的转换(),这样转换($PATH).< /P> < P>当您的数字字段包含一个小数值时,PHP实际上会认为它是“代码>字符串,而不是<代码>编号<代码>,因此,在使用它执行算术之前,需要将其转换为一个
数字
由于您已更新了问题的要求,我已修改了我的答案,以包含两个函数。我已经编写了一个函数将小数转换为小数,另一个函数将小数转换为小数
您可以在此处看到一个工作示例:
你不是用0.1乘以100,而是用0.09999998
。。。当涉及到(int)时,它将像4.999
这样的数字转换为4
,因此当使用(int)计数时,您的结果1020636.999999999
变为1020636
当您关心所涉及的精度时,应使用bcmul()函数。它是一个“可选”扩展,但如果您关心精度,那么它很快就会被要求
示例:
multiplier = 100000000;
$value = 0.01020637;
echo bcmul($value, $multiplier, 0);
非常感谢。这非常有帮助。用这个,它乘以分数。现在我被困在3/4的分数上,它会自动变为1.5,但不是1/2的正确分数。如果你点击这里:当你点击3号或6号,在6号上,它应该是食谱数量的两倍,因此3/4->1 1/2(希望)你是受欢迎的。我以为你的问题是把一个分数乘以一个整数,期望得到的是十进制结果,而不是小数结果。您可能希望使用各种输入组合的预期输出更新问题。这实际上是一个更复杂的问题要解决。谢谢!我刚刚编辑了它,希望这次我更清楚。我也将继续对此进行研究。再次感谢你的帮助,这至少让我达到了我主要需要的程度。
<?php
function fractionToDecimal($fraction)
{
// Split fraction into whole number and fraction components
preg_match('/^(?P<whole>\d+)?\s?((?P<numerator>\d+)\/(?P<denominator>\d+))?$/', $fraction, $components);
// Extract whole number, numerator, and denominator components
$whole = $components['whole'] ?: 0;
$numerator = $components['numerator'] ?: 0;
$denominator = $components['denominator'] ?: 0;
// Create decimal value
$decimal = $whole;
$numerator && $denominator && $decimal += ($numerator/$denominator);
return $decimal;
}
function decimalToFraction($decimal)
{
// Determine decimal precision and extrapolate multiplier required to convert to integer
$precision = strpos(strrev($decimal), '.') ?: 0;
$multiplier = pow(10, $precision);
// Calculate initial numerator and denominator
$numerator = $decimal * $multiplier;
$denominator = 1 * $multiplier;
// Extract whole number from numerator
$whole = floor($numerator / $denominator);
$numerator = $numerator % $denominator;
// Find greatest common divisor between numerator and denominator and reduce accordingly
$factor = gmp_intval(gmp_gcd($numerator, $denominator));
$numerator /= $factor;
$denominator /= $factor;
// Create fraction value
$fraction = [];
$whole && $fraction[] = $whole;
$numerator && $fraction[] = "{$numerator}/{$denominator}";
return implode(' ', $fraction);
}
// Examples
var_dump(fractionToDecimal('1/25')); // 0.04
var_dump(fractionToDecimal('2 3/4')); // 2.75
var_dump(fractionToDecimal('6/4')); // 1.5
var_dump(decimalToFraction(1.375)); // 1 3/8
var_dump(decimalToFraction(3)); // 3
var_dump(decimalToFraction(2.875)); // 2 7/8
multiplier = 100000000;
$value = 0.01020637;
echo bcmul($value, $multiplier, 0);