Format decimal places in PHP using point and comma

Asked

Viewed 5,179 times

2

I have a payment module where I must pass the data without decimals, only number. Examples:

10,00 = 1000

100,30 = 10030

1.000,00 = 100000

I researched some function in php that format decimal places, but the tests I performed did not go as expected, someone could help me in this question?

3 answers

2

2


In case, what you need is very simple, remove any value other than number:

$entrada = '100,30'; 

echo preg_replace('/[^0-9]+/','',$entrada);

Example in IDEONE

  • worked perfectly, but I think the question got confused, in case it would be the opposite I have the value without point and comma and want to format using decimals.

0

for comma use:

$numero = '100,30';

$SemVirgula  = str_replace(',', '', $numero );

for point use:

$numero = '100.30';

$SemVirgula  = str_replace('.', '', $numero );

For both use:

$numero = '2.100,30';

$SemVirgula  = str_replace(',', '', $numero );

$SemPonto  = str_replace('.', '', $SemVirgula );

Another way to remove point and comma:

$numero = '2.100,30';

$substituir = array(',', '.');

$substituidos = array('', '');

$NumFormatado = str_replace($substituir, $substituidos, $numero);
  • worked perfectly, but I think the question got confused, in case it would be the opposite I have the value without point and comma and want to format using decimals.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.