Is there a class or function to format currency in an input text?

0

I need to make a form to register products to a database, I wanted to know if there is any function so that when writing for example: 10 Leave like this: $ 10.00.

    
asked by GERMAN SOSA 28.04.2018 в 19:52
source

2 answers

0

You could try to concatenate a '$' before the number entered, and make a check if the number does not have decimals to add '.00'

A basic function:

function moneda($moneda) {
    if (strpos($moneda,'.')) {
        $str = '$'.$moneda;
    } else {

    $str = '$'.$moneda.'.00';
    }
    return $str;
}

Surely there is a better way to do it but maybe it will help you.

    
answered by 28.04.2018 в 19:58
0

If you use a version of PHP greater than 4.3.0 you can use the function money_format This function takes two parameters, the format and a numerical value.

To put the sign $ and two decimals, you could do the following:

$numero = 125.236;
echo money_format('$%.2n', $numero); // $125.24

"$" would be an arbitrary string and "% .2n" would be the value with 2 decimals.

Another option to put the sign $ would be using the function setlocale :

setlocale(LC_MONETARY, 'en_US.UTF-8');
$numero = 125;
echo money_format('%.2n', $numero); // $125.00
    
answered by 28.04.2018 в 21:26