Error number_format

0

Good evening guys.

I have this function to convert American value to Brazilian

function Real($valor){
    $valor_real = number_format($valor,2, ',', '.');
    return $valor_real;
}

It works fine, but it will display houses with thousands Ex. 1,250.00 it displays only 1,00

Can you help me?

I call the function this way

R$ <?=Real($lnP['produto_atributo_preco']); ?>

In mysql the field is as DECIMAL (10,2) was as VARCHAR and even then it has this error

Thank you

    
asked by anonymous 02.07.2017 / 05:55

2 answers

0

Commas as a thousand separator (American format) confuse number_format . Remove and leave only the dot as a decimal separator:

$valor = '1,250.00';
$valor_real = number_format($valor,2, ',', '.');

echo $valor_real; // 1,00

$valor = str_replace(',', '', $valor); // 1250.00
$valor_real = number_format($valor,2, ',', '.');

echo $valor_real; // 1.250,00

Example

    
02.07.2017 / 15:54
1

You can not write to the bank so 1,250.00 with DECIMAL (10,2) no, if you try to enter this value it will write to bank 1.00 and when you apply its function on it becomes 1.00 In this case test here

For DECIMAL fields (10,2), at the time of the insert it must be in these formats 1250.00 or 123456.00 or 1234567.89 etc, ie dot separator (.) only in the decimal point.

Another solution (a la gambiarra) is to use Varchar for the field.

    
02.07.2017 / 17:00