Jump to content

Having trouble with number_format


jetlife76

Recommended Posts

Trying to get my output to to show 2 decimal places but keep getting syntax errors please help.

Here's the code i have.

 

<?php
// Input Data From Form
$Cost = $_POST['fielda'];
//$Cost2 = number_format(,2);
//Set Current Date & Shipping Date (15 days)
$Date = date('l, F d, Y');
$ShipDate = date('F d, Y',strtotime('+ 15 days'));

$Tax = ($Cost * .06);
$Ship = 0;
//$Sub = ($Cost + $Tax);

//Calculate Shipping
if ($Cost <= 25.99 ){
   $Ship = 3.00; }
if ($Cost >= 26.00 && $Cost <= 50.99 ){
   $Ship = 4.00;}
if ($Cost >= 51.00 && $Cost <= 75.00) {
   $Ship = 5.00;}
if ($Cost > 75.00) {
   $Ship = 6.00;}
   
   //Calculate Order Total
$Total = ($Cost + $Tax + $Ship);

print " Date: $Date<br><br>";
print " Cost : $$Cost<br><br> Tax: $$Tax<br><br> Shipping: $$Ship<br><br> Total: $$Total<br><br>";
print "Estimated Ship Date is $ShipDate<br> " ;

?>

 

MOD EDIT: code tags added.

Link to comment
Share on other sites

<?php
// Input Data From Form
$Cost = $_POST['fielda'];
//$Cost2 = number_format(,2);
//Set Current Date & Shipping Date (15 days)
$Date = date('l, F d, Y');
$ShipDate = date('F d, Y',strtotime('+ 15 days'));

$Tax = ($Cost * .06);
$Ship = 0;
//$Sub = ($Cost + $Tax);

//Calculate Shipping
if ($Cost <= 25.99 ){
   $Ship = 3.00; }
if ($Cost >= 26.00 && $Cost <= 50.99 ){
   $Ship = 4.00;}
if ($Cost >= 51.00 && $Cost <= 75.00) {
   $Ship = 5.00;}
if ($Cost > 75.00) {
   $Ship = 6.00;}
   
   //Calculate Order Total
$Total = ($Cost + $Tax + $Ship);

print " Date: $Date<br><br>";
print number_format (" Cost : $$Cost<br><br> Tax: $$Tax<br><br> Shipping: $$Ship<br><br> Total: $$Total<br><br>",2);
print "Estimated Ship Date is $ShipDate<br> " ;

?>

 

MOD EDIT: code tags added.

 

Link to comment
Share on other sites

You can't feed an entire string to the number_format function like that. It would have no idea how to operate on it, as it expects a float/double value. You need to format each number individually, either prior to echoing the string, or or by concatenating the output into the string as it's echoed. Doing it prior would result in more readable code.

 

$number1 = 123.45678;
$number2 = 456.78901;

$number1 = number_format($number1, 2);
$number2 = number_format($number2, 2);

echo "Number1 is: $number1, and Number2 is: $number2";

 

I also see no reason for you to be using variable variables here.

Cost : $$Cost<br><br> Tax: $$Tax<br><br> Shipping: $$Ship<br><br> Total: $$Total<br><br>

Link to comment
Share on other sites

ok i've done that but now it says lines 16 & 18 have undefined variables, Ship and Tax, i thought they were defined aleady

<?php
// Input Data From Form
$Cost = $_POST['fielda'];

//Set Current Date & Shipping Date (15 days)
$Date = date('l, F d, Y');
$ShipDate = date('F d, Y',strtotime('+ 15 days'));

$Cost2 = number_format($Cost,2);
$Tax2 = number_format($Tax,2);
$Tax = ($Cost * .06);
$Ship2 = number_format($Ship,2);
$Ship = 0;

//$Sub = ($Cost + $Tax);

//Calculate Shipping
if ($Cost <= 25.99 ){
$Ship = 3.00; }
if ($Cost >= 26.00 && $Cost <= 50.99 ){
$Ship = 4.00;}
if ($Cost >= 51.00 && $Cost <= 75.00) {
$Ship = 5.00;}
if ($Cost > 75.00) {
$Ship = 6.00;}

//Calculate Order Total
$Total = ($Cost + $Tax + $Ship);

$Total2 = number_format($Total,2);
print "Date: $Date<br><br>";
print "Cost: $$Cost2<br><br>";
print "Tax: $$Tax2<br><br>";
print "Shipping: $$Ship2<br><br> $$Total<br><br>";
print "Total: $$Total2<br><br>";
print "Estimated Ship Date is $ShipDate<br> " ;
?>

Link to comment
Share on other sites

Since i have my shipping date to automatically configure 15 days after the order date, i need it to adjust to a leap year, in case the order is made the last day in feb.(29) on a leap

 

And, how are you determining 15 days? You shouldn't need any special handling for leap years if you did it right. It would just "work".

 

//Create timestamp 15 days in future
$ship_date_ts = str_to_time('+15 days');
//Format the ship date
$formatted_ship_date = date('m-d-Y', $ship_date_ts);

Link to comment
Share on other sites

Ok if anyone can help, i need to put my shipping calculations in a programmer defined function, any help, i kno it's something simple just can't get my brain started this morning.

You seem to be making less sense the more you explain things.  Copy and paste?  Call a function?  make a web service?  We have no idea what you're talking about.

 

Did you read what was said about leap years?  PHP handles them for you.

Link to comment
Share on other sites

ok here's what i got, still don't truly understand the "function" though.

 

<?php
// Input Data From Form
$Cost = $_POST['fielda'];
//Calculate tax 
$Tax = ($Cost * .06);

$Ship = 0;

//Set Current Date & Shipping Date (15 days)
$Date = date('l, F d, Y');
$ShipDate = date('F d, Y',strtotime('+ 15 days'));

//Calculate Shipping
function shipping_cost($Cost) {

if ($Cost > 0 && $Cost <= 25.00 ){
$Ship = 3.00; }
if ($Cost >= 25.01 && $Cost <= 50.00 ){
$Ship = 4.00;}
if ($Cost >= 50.01 && $Cost <= 75.00) {
$Ship = 5.00;}
if ($Cost > 75.00) {
$Ship = 6.00;}

return $Ship; }

//Calculate Order Total
$Total = ($Cost + $Tax + $Ship);


$Cost2 = number_format($Cost,2);
$Tax2 = number_format($Tax,2);
$Ship2 = number_format($Ship,2);
$Total2 = number_format($Total,2);


    print "Date: $Date<br><br>";
    print "Cost: $$Cost2<br><br>";
     print "Tax: $$Tax2<br><br>";
print "Ship: $$Ship2<br><br>";
   print "Total: $$Total2<br><br>";
print "Estimated Ship Date is $ShipDate<br> " ;
?>

Link to comment
Share on other sites

As much as it pains me to say it: read the manual.  Functions are one of the most basic concepts of computer science.  You have to call a function in order for it to do anything and (in your case) store the return value somewhere.  Ask your professor if you need help with this, functions are very simple to understand.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.