English 中文(简体)
不必要四舍五入的数值
原标题:values unnecessarily rounded

我有一些数据库数字, 我用它来做一些简单的数学。 出于某种原因, 我无法将总数从四舍五入到最接近的美元。 不过, 我还需要包含美分信息。 我肯定每个项目条目在数据库中都包含小数点后两个位数 。

if (strpos($row2["itemDiscount"], % ) !== false) {
    $itemDiscount = $row2["itemDiscount"];
    $itemDetailTotalUnformatted = $row2["itemQuantity"]*($itemPrice*(1-($itemDiscount/100)));
}
else {
    $itemDetailTotalUnformatted = $row2["itemQuantity"]*($row2["itemPrice"]-$row2["itemDiscount"]);
}
$itemDetailTotal = number_format($itemDetailTotalUnformatted, 2,  . ,   );
echo $itemDetailTotal;

var_dump( $row2) :

50.00array(6) {
[0]=>
string(1) "2"
"itemQuantity"]=>
string(1) "2"
[1]=>
string(5) "30.00"
[itemPrice]=>
string(1) "30.00"
[2]=>
string(4) "5.00"
[itemPrice]=>
string(4) "5.00"
最佳回答

处理货币时, ALWAYS 以整数方式工作。 省下美分价格, 处理美分价格, 只有在最后, 你才能将结果除以100 来展示 。

其原因是,Ints具有完全精确性(最高为淫秽高值,以浮点数取代以浮点数),而浮点数则不是。 PHP 没有固定点类型 。

一旦你这样做,你的圆形问题 可能会消失。

问题回答

暂无回答




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签