Help - Search - Members - Calendar
Full Version: PHP: Removing the leading zero's from a decimal
Invision Power Services > Community Forums > Community Web Design and Coding
Joshua Jones
I am trying to build a conversion calculator fractions to decimals.

I have this an it works except it always puts in a leading zero. if I want to add a number before the anwer like 2 3/4 the result will be 20.75.

CODE
<?php
/// Get Numbers and Set Variables ///
$numerator1 = $_POST['numerator1'];
$denominator1 = $_POST['denominator1'];
$numerator2 = $_POST['numerator2'];
$denominator2 = $_POST['denominator2'];
$number3 = $_POST['number3'];
$number4 = $_POST['number4'];

/// Math ///
$mult1 = $numerator1 / $denominator1;
$mult2 = $numerator2 / $denominator2;

/// Convert to 3 places after decimal ///
$final1 = number_format($mult1,3);
$final2 = number_format($mult2,3);

/// Print Final Numbers ///
print "<p>$number3$final1</p>";
print "<p>$number4$final2</p>";
?>


Is there away to remove the 0 from the 0.75 so it will output 2.75?

Thanks,
Josh
Joshua Jones
Nevermind

All I had to do was add. rolleyes.gif

CODE
<?php
/// Get Numbers and Set Variables ///
$numerator1 = $_POST['numerator1'];
$denominator1 = $_POST['denominator1'];
$numerator2 = $_POST['numerator2'];
$denominator2 = $_POST['denominator2'];
$number3 = $_POST['number3'];
$number4 = $_POST['number4'];

/// Math ///
$mult1 = $numerator1 / $denominator1;
$mult2 = $numerator2 / $denominator2;

/// Add Full Number ///
$result1 = $number3 + $mult1;
$result2 = $number4 + $mult2;

/// Convert to 3 places after decimal ///
$final1 = number_format($result1,3);
$final2 = number_format($result2,3);

//Print Final Numbers
print "<p>$final1</p>";
print "<p>$final2</p>";
?>
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.