价格为小数为32nds的价格



我正在尝试为此编码一个函数:从小数为小数的价格转换为32nds :像这样的计算器:http://www.iotafinance.com/en/fractional-decimal-price-conversion-calculator.html

i在PHP中代码一个函数,但这对所有情况都不起作用:例子 :对于 4.078 十进制,我应该有32nds 中的 4.024但是我有4.025

有优雅的功能吗?我认为我的解决方案不好,并且不适用于所有情况。

非常感谢

PHP中的代码:

<?php
function convert32eme($dec) {
    // Init 
    $pos2 = "";
    $pos3 = "";
    $pre = "";
    $i = 0;
    $aNumberDecimal = explode(".", $dec);
    $iEntier = $aNumberDecimal[0];
    $fDecimal = @$aNumberDecimal[1];
    // Two first décimales in 32eme
    if(isset($fDecimal)) {
        $pos2 = '0.'.(int)$fDecimal;
        while(substr($fDecimal, $i++, 1) == 0) {
            $pre .= "0";
        }
        $pos2 = $pre.$pos2*32; // Multiply 32
        $aNumberDecimal2 = explode(".", $pos2);
        $pos2 = $aNumberDecimal2[0]; // 2 first decimal
        // Third decimales in 8eme of 32eme
        $iEntier2 = $aNumberDecimal2[0];
        $fDecimal2 = @$aNumberDecimal2[1];
        if(isset($fDecimal2)) {
            $c = '0.'.(int)$fDecimal2;
            $pos3 = round($c*8); // Multiply 8
            echo '<br/>Pos3 : '.$pos3;
        }
    }
    // Final
    $fDecimalFinal = ($pos2).($pos3);
    echo '<br/>'.$fDecimalFinal;
    $aFoo = explode('.', round("0.".$fDecimalFinal, 3));
    $fDecimalFinal = @$aFoo[1];
    if(!$fDecimalFinal) {
        $fDecimalFinal = "00";
    }
    return $iEntier.'.'.$fDecimalFinal;
}
function display($dec, $correc, $result) {
    echo '----------------<br/>'.$dec.' - '.$correc;
    if($result == $correc) {
        echo ' <span style="color: green">OK</span>';
    } else
    {
        echo ' <span style="color: red">KO</span>';
    }
    echo '<br/>';
}
function useless($dec, $correc) {
    $result = convert32eme($dec);
    display($dec, $correc, $result);
    return $result;
}
echo '-> ' . useless(100.515625, 100.164).'<br/>'; // OK
echo '-> ' . useless(100.9609375, 100.306).'<br/>'; // OK
echo '-> ' . useless(108.0859, 108.027).'<br/>'; // OK
echo '-> ' . useless(100.03125, 100.01).'<br/>'; // OK
echo '-> ' . useless(100.5, 100.16).'<br/>'; // OK
echo '-> ' . useless(100.00, 100.00).'<br/>'; // OK
echo '-> ' . useless(1000.096, 1000.031).'<br/>'; // OK
echo '-> ' . useless(94949.4564879, 94949.145).'<br/>'; // OK
echo '-> ' . useless(0.454, 0.144).'<br/>'; // OK
echo '-> ' . useless(4.0035, 4.001).'<br/>'; // OK
echo '-> ' . useless(4.0078, 4.002).'<br/>'; // OK
echo '-> ' . useless(4.078, 4.024).'<br/>'; // dont't work
echo '-> ' . useless(4.071, 4.022).'<br/>'; // dont't work

您的转换功能似乎非常复杂,因为您已经做出了在字符串上工作而不是在数字上工作的错误选择。如果您使用数字,则不必担心点或这种事情之后的零数。您只需要计算并弄清结果:

function convert32($num) {
    $intpart = intval($num);
    return round($intpart + ($num - $intpart)/3.125, 3);
}

与第八部分:

function convert32($num) {
    $intpart = intval($num);
    $dec32 = ($num - $intpart)*.32;
    $dec8 = ($dec32 - floor($dec32*100)/100)*.8;
    $dec32 = floor($dec32*100)/100;
    return round($intpart + $dec32 + $dec8, 3);
}

在法语中

trente-deuxième是缩写的 32e ( 32 e ,而不是(32eme

最新更新