Php 以小数表示的价格与以32 nds表示的价格

Php 以小数表示的价格与以32 nds表示的价格,php,algorithm,financial,Php,Algorithm,Financial,我正试图为此编写一个函数:从十进制价格转换为32nds价格: 像这个计算器: 我用PHP编写了一个函数,但这并不适用于所有情况: 例子: 对于4.078十进制,我应该在32nds中有4.024 但我有4.025 有优雅的功能吗?我认为我的解决方案不好,不能适用于所有情况 非常感谢 PHP中的代码: <?php function convert32eme($dec) { // Init $pos2 = ""; $pos3 = ""; $pre = "";

我正试图为此编写一个函数:从十进制价格转换为32nds价格: 像这个计算器:

我用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);
}

在法语中,如果可能的话,deuxième缩写为32e 32e,而不是32eme。

非常感谢Casimir et Hippolyte