2

ですから、お金とセントへの変換とセントからの変換に関して複数の質問があることは知っています. 別の質問もしましたが、少し違う質問をしたいので、重複がないことを願っています。

そこで、Dollar Value を取得して CENTS に送信する関数を作成しました。しかし、コードに少し問題があると思います。少し調整できることを願っています。

$money4 = "10.0001";
// Converted to cents, as you can see it's slightly off. 
$money41 = "1001";
// So when "1001", get's put in the database, and then I return it back as a Money variable. 
// We get, "$10.01"... but what I have now is a leak in my amounts... as it rounded up to the second point. 

だから私がやったことをするために、私はこれをするために私が作った関数に慣れてきました。

// This essentially gets a DOLLAR figure, or the CENT's Figure if requested.    
function stripMoney($value, $position = 0, $returnAs = "") 
{   
    // Does it even have a decimal?
    if(isset($value) && strstr($value, ".")) {

        // Strip out everything but numbers, decimals and negative
        $value    = preg_replace("/([^0-9\.\-])/i","",$value);
        $decimals = explode(".", $value);

        // Return Dollars as default
        return ($returnAs == "int" ? (int)$decimals[$position] : $decimals[$position]);

    } elseif(isset($value)) {
        // If no decimals, lets just return a solid number
        $value = preg_replace("/([^0-9\.\-])/i","",$value);
        return ($returnAs == "int" ? (int)$value : $value);
    }
}

次に使用する関数は、CENTS を生成するか、それをドルとして返すことです。

function convertCents($money, $cents = NULL, $toCents = TRUE)
{
    if(isset($money)) {
        if($toCents == TRUE) {
            // Convert dollars to cents
            $totalCents = $money * 100;
            // If we have any cents, lets add them on as well
            if(isset($cents)) {
                $centsCount = strlen($cents);
                // In case someone inputs, $1.1
                // We add a zero to the end of the var to make it accurate
                if($centsCount < 2) {
                    $cents = "{$cents}0";   
                }
                // Add the cents together
                $totalCents = $totalCents + $cents;
            }
            // Return total cents
            return $totalCents;

        } else {
            // Convert cents to dollars
            $totalDollars = $money / 100;
            return $totalDollars;
        }
    }
}

そして、すべてをまとめる最後の機能。したがって、基本的に 2 つの関数をマージするために 1 つの関数を使用するだけです。

function convertMoney($value, $toCents = TRUE) {
    if(isset($value) && strstr($value, ".")) {
        return convertCents(stripMoney($value, 0), stripMoney($value, 1), $toCents);

    } elseif(!empty($value)) {
        return convertCents(stripMoney($value, 0), NULL, $toCents);
    }
}

私がやったことはやり過ぎかもしれませんが、私が見ることができるこの1つの詳細を除いて、それはかなりしっかりしていると思います.

誰でもこれらの調整を手伝ってもらえますか?

4

2 に答える 2

3

正確な答えが必要な場合は、浮動小数点演算を使用しないでください。これは、PHP だけでなく、ほぼすべての言語に当てはまります。PHP マニュアルの大きな警告を読んでください。

代わりに、 BC MathまたはGMP 拡張を調べてください。後者は整数でのみ機能するため、おそらく BC Math に最も関心があるでしょう。

于 2011-07-09T20:50:27.757 に答える
2

money_formatはあなたが探していた関数だと思います...

<?php

$number = 1234.56;

// let's print the international format for the en_US locale
setlocale(LC_MONETARY, 'en_US');
echo money_format('%i', $number) . "\n";
// USD 1,234.56

// Italian national format with 2 decimals`
setlocale(LC_MONETARY, 'it_IT');
echo money_format('%.2n', $number) . "\n";
// Eu 1.234,56

?>
于 2011-07-09T20:51:15.243 に答える