Hi guys, is there a function in php which makes it possible to extract the decimal part of a float number?
For example I've got : $x= 7.4 how do I get the decimal part? (that should return 0.4..)
Thanks!
extract the float part from a floating..?
$x = 7.4;
$explode = explode('.', $x);
echo $explode[1];
devinemke wrote:
$x = 7.4; $explode = explode('.', $x); echo $explode[1];
you're right! what a dumb question
Thanks!
See also [man]floor[/man], [man]ceil[/man], and [man]intval[/man].
5 years later
Yup, simple. Why didn't I think of this?:o
However, before exploding it, I changed the integer to a dollar amount. That took care of the rounding. Here is what I did, in case somebody wants to know:
function convert_number($number)
{
if (($number < 0) || ($number > 999999999))
{
return "$number";
}
$Gn = floor($number / 1000000); /* Millions (giga) */
$number -= $Gn * 1000000;
$kn = floor($number / 1000); /* Thousands (kilo) */
$number -= $kn * 1000;
$Hn = floor($number / 100); /* Hundreds (hecto) */
$number -= $Hn * 100;
$Dn = floor($number / 10); /* Tens (deca) */
$n = $number % 10; /* Ones */
$res = "";
if ($Gn)
{
$res .= convert_number($Gn) . " Million";
}
if ($kn)
{
$res .= (empty($res) ? "" : " ") .
convert_number($kn) . " Thousand";
}
if ($Hn)
{
$res .= (empty($res) ? "" : " ") .
convert_number($Hn) . " Hundred";
}
$ones = array("", "One", "Two", "Three", "Four", "Five", "Six",
"Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen",
"Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eightteen",
"Nineteen");
$tens = array("", "", "Twenty", "Thirty", "Fourty", "Fifty", "Sixty",
"Seventy", "Eigthy", "Ninety");
if ($Dn || $n)
{
if (!empty($res))
{
// $res .= " and ";
$res .= " ";
}
if ($Dn < 2)
{
$res .= $ones[$Dn * 10 + $n];
}
else
{
$res .= $tens[$Dn];
if ($n)
{
$res .= "-" . $ones[$n];
}
}
}
if (empty($res))
{
$res = "zero";
}
return $res;
}
$USDollar = number_format($TotNet, 2,'.',','); // put it in decimal format, rounded
$printTotNet = convert_number($TotNet); //convert to words (see function above)
$table .= '*********** ';
$x = $USDollar;
$explode = explode('.', $x); //separate the cents
$printDolCents = $printTotNet . ' Dollars and ' . $explode[1] . ' Cents ***';
$table .= $printDolCents; // print the line with dollars words and cents in numerals
Thanks for your help