PHP计算结果未显示正确的货币格式

时间:2015-02-13 13:28:34

标签: php

在下面的代码中,我试图得到结果:

$time_base_cost_day;

$time_base_cost_month;

显示正确的货币逗号分隔符,例如$1,456.00$100,456.00

我尝试过几个例子,但不确定我在哪里没有正确添加功能。

感谢您的帮助

<?php 
  $choose_industry = $_POST['choose_industry'];
  $company_name = $_POST['company_name'];
  $hourly_rate = $_POST['hourly_rate'];
  $billable_rate = $_POST['billable_rate'];
 $working_days = $_POST['working_days'];
 $wasted_time_per_day = $_POST['wasted_time_per_day'];
  $no_of_trucks = $_POST['no_of_trucks'];
 $fuel_price_per_liter = $_POST['fuel_price_per_liter'];
 $fuel_wasted_per_hr = $_POST['fuel_wasted_per_hr'];

 $time_base_cost_day = $hourly_rate*$wasted_time_per_day;
  $time_base_cost_month = $time_base_cost_day*$working_days;

 $productivity_costs_day = $billable_rate*$wasted_time_per_day;
  $productivity_costs_month = $productivity_costs_day*$working_days;

 $fuel_base_cost_day = $wasted_time_per_day*$fuel_price_per_liter*$fuel_wasted_per_hr;
 $fuel_base_cost_month = $fuel_base_cost_day*$working_days;
  $monthly_cost_per_unit = 35;
  $lost_per_vehicle_per_month = ($time_base_cost_month+$productivity_costs_month+$fuel_base_cost_month) - $monthly_cost_per_unit ;

 $monthly_savings_per_truck = $lost_per_vehicle_per_month;//Monthly Savings = (Monthly Time Based Costs + Monthly Productivity Costs + Monthly Fuel Based Costs) – Monthly Costs per Unit.

 $monthly_savings_per_fleet = $monthly_savings_per_truck*$no_of_trucks;


   $monthly_cost_per_fleet = $no_of_trucks*$monthly_cost_per_unit;



    $link = mysql_connect('##', '##', '##');
    mysql_select_db('##', $link);


 if( (!empty($choose_industry)) && (!empty($company_name)) && (!empty($hourly_rate)) && (!empty($billable_rate)) && (!empty($working_days)) && (!empty($wasted_time_per_day)) && (!empty($no_of_trucks)) && (!empty($fuel_price_per_liter)) && (!empty($fuel_wasted_per_hr)) && (!empty($time_base_cost_day)) && (!empty($time_base_cost_month)) && (!empty($productivity_costs_day)) && (!empty($productivity_costs_month)) && (!empty($fuel_base_cost_day)) && (!empty($fuel_base_cost_month)) && (!empty($monthly_savings_per_truck)) && (!empty($monthly_savings_per_fleet)) ){

 $result = mysql_query("INSERT into wp_calculate (id, choose_industry, company_name, hourly_rate, billable_rate, working_days, wasted_time_per_day, no_of_trucks, fuel_price_per_liter, fuel_wasted_per_hr, time_base_cost_day, time_base_cost_month, productivity_costs_day, productivity_costs_month, fuel_base_cost_day, fuel_base_cost_month, monthly_savings_per_truck, monthly_savings_per_fleet, monthly_cost_per_unit, monthly_cost_per_fleet, Date ) values ('', '$choose_industry','$company_name', '$hourly_rate', '$billable_rate', '$working_days', '$wasted_time_per_day', '$no_of_trucks', '$fuel_price_per_liter', '$fuel_wasted_per_hr', '$time_base_cost_day', '$time_base_cost_month', '$productivity_costs_day', '$productivity_costs_month', '$fuel_base_cost_day', '$fuel_base_cost_month', '$monthly_savings_per_truck', '$monthly_savings_per_fleet', '$monthly_cost_per_unit', '$monthly_cost_per_fleet', now() )");
 $last_id = mysql_insert_id();

 $sql = "UPDATE wp_calculate SET pdf_link='/main/downloadpdf.php?user_id=$last_id' WHERE id=$last_id";
 $retval = mysql_query($sql);

?>


 <div class="offset-wrapper push-down3" id="cost_based">
<!--<div class="span6"></div>
<div class="span6"></div>-->
<div class="row cost_row" id="cost_row">
   <div class="custom-calc-inner-2 wow bounceInLeft" data-wow-duration="3s" data-wow-delay="1s">
        <h4>Time Based Costs</h4>
        <p>Cost of wasted time (per driver)</p>
    <div class="result">        
        <div class="pos-left">
        <p class="value">DAY</p><p class="lbl">$<?php echo $time_base_cost_day; ?></p>
        </div>
        <div class="pos-right">
        <p class="lbl">$<?php echo $time_base_cost_month; ?></p><p class="value">MONTH</p>
        </div>
        </div>
        <div class="clear"></div>
    </div>

1 个答案:

答案 0 :(得分:1)

echo number_format($time_base_cost_day,2);

echo number_format($time_base_cost_month,2);

2是多少位小数,你仍然可以添加逗号或点,如:

  • 默认模式是您想要的(逗号分隔千位和小数点,所以您不需要在下面使用此部分)

    echo number_format($ time_base_cost_month,2,',','。'); //返回1.456,00

来源:http://php.net/manual/en/function.number-format.php