Page 1 of 1
number formatting
Posted: Wed Apr 12, 2006 10:47 am
by Neller
ive created a small shop in php but im having trouble with what to use for the numbers
i need to store all kinds of prices like:
2.99
20.99
200.99
i tried normal INT and used number_format on the page itself but that displays 20.99 as 2,099 so i tried using FLOAT in the database but that cuts the 0 of any prices like 20.90 displays as 20.9 i will keep on checking the manual and trying different things but if anyone no's the best way please let me no
thanks
Posted: Wed Apr 12, 2006 10:54 am
by hawleyjr
Posted: Wed Apr 12, 2006 10:57 am
by Neller
thanks ill check it out, after i made my post i tried using DOUBLE in the database and it seems to be working fine but ill still check out that function
thanks again
Posted: Wed Apr 12, 2006 2:10 pm
by pickle
You should be able to store it in the DB as a floating point type (ie: float). If it truncates - doesn't matter because number_format() can fix that:
Code: Select all
$number = 20.9;
$pretty_number = number_format($number,2);
//$pretty_number is now 20.90
hope this function might help you
Posted: Wed Apr 12, 2006 11:58 pm
by dibyendrah
Code: Select all
// FormatNumber
/*
FormatNumber(Expression[,NumDigitsAfterDecimal [,IncludeLeadingDigit
[,UseParensForNegativeNumbers [,GroupDigits]]]])
NumDigitsAfterDecimal is the numeric value indicating how many places to the
right of the decimal are displayed
-1 Use Default
The IncludeLeadingDigit, UseParensForNegativeNumbers, and GroupDigits
arguments have the following settings:
-1 True
0 False
-2 Use Default
*/
function FormatNumber($amount, $NumDigitsAfterDecimal, $IncludeLeadingDigit, $UseParensForNegativeNumbers, $GroupDigits)
{
// export the values returned by localeconv into the local scope
if (function_exists("localeconv")) extract(localeconv());
// set defaults if locale is not set
if (empty($currency_symbol)) $currency_symbol = DEFAULT_CURRENCY_SYMBOL;
if (empty($mon_decimal_point)) $mon_decimal_point = DEFAULT_MON_DECIMAL_POINT;
if (empty($mon_thousands_sep)) $mon_thousands_sep = DEFAULT_MON_THOUSANDS_SEP;
if (empty($positive_sign)) $positive_sign = DEFAULT_POSITIVE_SIGN;
if (empty($negative_sign)) $negative_sign = DEFAULT_NEGATIVE_SIGN;
if (empty($frac_digits) || $frac_digits == CHAR_MAX) $frac_digits = DEFAULT_FRAC_DIGITS;
if (empty($p_cs_precedes) || $p_cs_precedes == CHAR_MAX) $p_cs_precedes = DEFAULT_P_CS_PRECEDES;
if (empty($p_sep_by_space) || $p_sep_by_space == CHAR_MAX) $p_sep_by_space = DEFAULT_P_SEP_BY_SPACE;
if (empty($n_cs_precedes) || $n_cs_precedes == CHAR_MAX) $n_cs_precedes = DEFAULT_N_CS_PRECEDES;
if (empty($n_sep_by_space) || $n_sep_by_space == CHAR_MAX) $n_sep_by_space = DEFAULT_N_SEP_BY_SPACE;
if (empty($p_sign_posn) || $p_sign_posn == CHAR_MAX) $p_sign_posn = DEFAULT_P_SIGN_POSN;
if (empty($n_sign_posn) || $n_sign_posn == CHAR_MAX) $n_sign_posn = DEFAULT_N_SIGN_POSN;
// check $NumDigitsAfterDecimal
if ($NumDigitsAfterDecimal > -1)
$frac_digits = $NumDigitsAfterDecimal;
// check $UseParensForNegativeNumbers
if ($UseParensForNegativeNumbers == -1) {
$n_sign_posn = 0;
if ($p_sign_posn == 0) {
if (DEFAULT_P_SIGN_POSN != 0)
$p_sign_posn = DEFAULT_P_SIGN_POSN;
else
$p_sign_posn = 3;
}
} elseif ($UseParensForNegativeNumbers == 0) {
if ($n_sign_posn == 0)
if (DEFAULT_P_SIGN_POSN != 0)
$n_sign_posn = DEFAULT_P_SIGN_POSN;
else
$n_sign_posn = 3;
}
// check $GroupDigits
if ($GroupDigits == -1) {
$mon_thousands_sep = DEFAULT_MON_THOUSANDS_SEP;
} elseif ($GroupDigits == 0) {
$mon_thousands_sep = "";
}
// start by formatting the unsigned number
$number = number_format(abs($amount),
$frac_digits,
$mon_decimal_point,
$mon_thousands_sep);
// check $IncludeLeadingDigit
if ($IncludeLeadingDigit == 0) {
if (substr($number, 0, 2) == "0.")
$number = substr($number, 1, strlen($number)-1);
}
if ($amount < 0) {
$sign = $negative_sign;
$key = $n_sign_posn;
} else {
$sign = $positive_sign;
$key = $p_sign_posn;
}
$formats = array(
'0' => '(%s)',
'1' => $sign . '%s',
'2' => $sign . '%s',
'3' => $sign . '%s',
'4' => $sign . '%s');
// lookup the key in the above array
return sprintf($formats[$key], $number);
}
//end function FormatNumber()
[/quote]
Posted: Thu Apr 13, 2006 1:53 am
by wyred
DECIMAL seems to be the most used datatype for currency.
DECIMAL(x,y)
x = number of digits, e.g x=7: you can have 7 digits: $1234567
y = number of decimal places, use 2. So DECIMAL(7,2): $1234567.99
Back to PHP, just use number_format($price,2) to add commas.
Code: Select all
$price = '3567556.82';
echo number_format($price,2); //will give you 3,567,556.82