我試圖從總價中計算稅和價格,我已經創建儘可能基本上相同的代碼在PHP和VB.net下面,但我有顯然錯過了一些東西,我已經花了兩天時間在這個上面,並且讓我感到尷尬,因爲大多數情況下我會得到相同的結果,但是奇怪的結果是錯誤的,例如1;PHP和VB.net類似的代碼不同的結果
PHP結果
DVD光盤€11.17€55.83
Traxdata CD-R 52X€1.09€5.49
青根CD-R 52X€0.56€2.77
愛普生18青色€1.04€5.20
VB.NET結果
DVD光盤€11.17 55.83€
Traxdata CD-R 52X€1.10€5.48
青根CD-R 52X€0.56€2.77
愛普生18青色€1.04€5.20
我已經嘗試了所有我能想到的,我w非常感謝任何幫助。總價格是;
的DVD€67.00
Traxdata CD-R 52X€6.58
青根CD-R 52X€3.33
愛普生18青色€6.24
PHP
function formatdecimalcurrency($amount) {
$multipliedNumber = $amount * 100;
$integerMultipliedNumber = floor($multipliedNumber);
$twoDecimalResult = number_format((float) ($integerMultipliedNumber/
100),2);
return $twoDecimalResult;
}
function pricefromtotal($percent, $price){
$decprice = floatval($price);
return formatdecimalcurrency($decprice - taxfromtotal($percent,
$price));
}
function taxfromtotal($percent, $price){
$decpercent = floatval($percent);
$taxasdec = $decpercent/100 + 1;
$dectotal = floatval($price);
$decprice = formatdecimalcurrency($dectotal/$taxasdec);
$dectax = $dectotal - $decprice;
return formatdecimalcurrency($dectax);
}
VB.NET
Public Shared Function pricefromtotal(ByVal Percent As String, Price As Decimal)
Dim decprice As Decimal = CDec(Price)
Return decprice - taxfromtotal(Percent, Price)
End Function
Public Shared Function taxfromtotal(ByVal Percent As String, Price As Decimal)
Dim decpercent As Decimal
If Percent.IndexOf("%") > 0 Then
decpercent = Percent.Substring(0, Percent.Length - 1)
Else
decpercent = Percent
End If
Dim taxasdec As Decimal = decpercent/100 + 1
Dim dectotal As Decimal = CDec(Price)
Dim decprice As Decimal = MyFormatDecimalCurrency(dectotal/taxasdec)
Dim dectax As Decimal = dectotal - decprice
Return dectax
End Function
Public Shared Function MyFormatDecimalCurrency(ByVal amount As Decimal)
Dim multipliedNumber As Decimal = amount * 100
Dim integerMultipliedNumber As Decimal = Math.Round(multipliedNumber)
Dim twoDecimalResult As Decimal = FormatNumber(Convert.ToSingle(integerMultipliedNumber/100), 2)
Return twoDecimalResult
End Function
在VB中使用'round',但在PHP中使用'floor'。這可以解釋1美分的差異。嘗試http://php.net/manual/de/function.round.php,或將VB代碼切換到底層。 – jh1711
根據你的司法管轄區,可能有一個「法律」,告訴你如何收稅。你obv。應該使用這兩種語言。 – jh1711