title: Best practice to sum and format numbers to dollar values with 2 decimal points?
body: <p>Here's the code I came up with, I want to:</p>
  
  <ol>
  <li>sum price + shipping to a total</li>
  <li>always display formatted numbers with two decimal points</li>
  <li>have a dollar sign at the start for all three values</li>
  <li>display an alternative "free shipping" if the shipping value is zero.</li>
  </ol>
  
  <p>PHP:</p>
  
  <pre><code>$price0 = "356";
  $ship0 = "9.9";
  $tprice0 = $price0 + $ship0;
  
  $price1 = number_format((float)$price0, 2, '.', '');
  $ship1 = number_format((float)$ship0, 2, '.', '');
  $tprice1 = number_format((float)$tprice0, 2, '.', '');
  
  if($ship0 == 0) { $shipping1 = '&lt;span class="free"&gt;Free Shipping&lt;/span&gt;'; } else { $shipping1 = "$$ship1"; }
  </code></pre>
  
  <p>HTML:</p>
  
  <pre><code>Price: $&lt;?php echo $price1; ?&gt; 
  Shipping: &lt;?php echo $shipping1; ?&gt;
  Total: $&lt;?php echo $tprice?&gt;
  </code></pre>
  
  <p>Is this the best practice? (e.g, I'm using a double dollar sign), and is there a way to make the code cleaner / shorter?</p>
  
  <p><strong>EDIT:</strong>
  note that I want to have this practice for several products in the same page, so is there a way to make it shorter?</p>
  
  <pre><code>$price0b = "223";
  $ship0b = "5.5";
  $tprice0b = $price0b + $ship0b;
  
  $price1b = number_format((float)$price0b, 2, '.', '');
  $ship1b = number_format((float)$ship0b, 2, '.', '');
  $tprice1b = number_format((float)$tprice0b, 2, '.', '');
  
  if($ship0b == 0) { $shipping1b = '&lt;span class="free"&gt;Free Shipping&lt;/span&gt;'; } else { $shipping1b = "$$ship1b"; }
  </code></pre>
  
  <p>EDIT2:</p>
  
  <p>I'm getting a class not found error with this code:</p>
  
  <pre><code>&lt;?php
  
  $price = "356";
  $shipping = "0";
  $total_price = $price0 + $ship0;
  
  $oNumberFormatter = new NumberFormatter('en_US', NumberFormatter::PATTERN_DECIMAL, '¤ #,##0.00');
  ?&gt;  
  &lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;/head&gt;
  &lt;body&gt;
  Price: &lt;?= $oNumberFormatter-&gt;format( (float) $price ); ?&gt;
  Shipping: &lt;?= !$ship0b ? "&lt;span class=\"free\"&gt;Free Shipping&lt;/span&gt;" : $oNumberFormatter-&gt;format( (float) $shipping ); ?&gt;
  Total: &lt;?= $oNumberFormatter-&gt;format( (float) $total_price ); ?&gt;
  </code></pre>