Jump to content

printf function


CrimsonZ

Recommended Posts

How do get the $disc_total and round($TotalCost/12, 2) to work with printf?

 

   $disc_total = ", minus your $". $disc_rate . " discount,";
   $TotalCost = ($widget_rate - $disc_rate) * 1.06;


   printf("<p>You requested " . $_GET[quantity] . " widgets(s) at $%.2f each.</p>",$widget_cost);
   printf("<p>Your total with tax" . $disc_total . " comes to $%.2f.</p> ",$TotalCost);
   print "You may purchase the widget(s) in 12 monthly installments of $" .round($TotalCost/12, 2) . " each.";

Link to comment
https://forums.phpfreaks.com/topic/48398-printf-function/
Share on other sites

does this help any?

 

<?
if(isset($_GET['quantity'])){
  $quantity = $_GET['quantity'];
}else{
  $quantity = 1;
}

   $widget_rate = 20;
   $disc_rate = 11;
   $widget_cost = 10;
   $disc_total = ", minus your $". $disc_rate . " discount,";
   $TotalCost = (($widget_rate * $quantity) * 1.06) - $disc_rate;


   printf("<p>You requested " . $quantity . " widgets(s) at $%.2f each.</p>",$widget_cost);
   printf("<p>Your total with tax" . $disc_total . " comes to $%.2f.</p> ",$TotalCost);
   printf("<p>You may purchase the widget(s) in 12 monthly installments of $%.2f each.", round($TotalCost/12, 2));


?>

Link to comment
https://forums.phpfreaks.com/topic/48398-printf-function/#findComment-236685
Share on other sites

Alright, I got the $TotalCost working but not the $disc_total.

 

Tricky thing is, the discount is not always applied.

 

So I would like the code to look like this:

   printf("<p>Your total with tax $%.2f comes to $%.2f.</p> ",$disc_total,$TotalCost);

 

but when I do that it doesn't come out right since:

 

    $disc_total = ", minus your $". $disc_rate . " discount,";

 

any ideas?

 

Link to comment
https://forums.phpfreaks.com/topic/48398-printf-function/#findComment-236699
Share on other sites

That's because $disc_total is a string, not a numeric value, having a numeric value of zero.

 

try

<?php
$disc_total = sprintf(", minus your $%0.2f discount,", $disc_rate);
printf("<p>Your total with tax %s comes to $%.2f.</p> ",$disc_total,$TotalCost);
?>

Link to comment
https://forums.phpfreaks.com/topic/48398-printf-function/#findComment-236736
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.