"echo" shows decimal number with comma instead of dot

Asked

Viewed 66 times

0

I have this code in PHP 7:

$total_pedido = 123.90;
$frete_valor = 13.98;

$total_geral = ($total_pedido + $frete_valor);

echo ($total_geral); // Mostra 137,88

The result of the sum (137.88) which is correct, but giving a echo (without any treatment) he brings 137,88 with comma, shouldn’t come with stitch?

Due to this result with comma I have to treat the string before sending to a webservice.

This behavior is normal in PHP or is due to some configuration in php.ini?

  • 3

    It’s probably because of locale, but I don’t remember where PHP gets it from (whether it’s from the operating system or php.ini). Anyway, if you want to control how the number is formatted, regardless of the configuration of the locale, use number_format: https://answall.com/a/492324/112052

  • I think I’m wrong @hkotsubo, but maybe I’m on the outside anyway I don’t know such behavior in PHP, I say specifically in a simple mathematical operation this shouldn’t occur, so far as I know, the setlocale (or default_locale in php.ini) affects many things, but not the output of echo, in matter of numbers what it will affect is the localeconv(), for both LC_MONETARY and LC_NUMERIC

  • Marcelo, can print the print_r(localeconv()); in the same script and return the results to [Edit] the question?

  • @Guilhermenascimento I made a test here (PHP 7.4.3/Ubuntu 20) and if I do setlocale(LC_ALL, 'pt_BR.utf8'), the echo showcase 137,88. If you change to, for example, setlocale(LC_ALL, 'en_US.utf8'), then comes to show 137.88. But as I said, I don’t know exactly where PHP gets these configs from, so it could have been a coincidence or some specific config of my machine, I don’t know. In online IDE’s no difference, and my guess is that they don’t have the pt_BR locale installed, but again, it’s just my guess...

1 answer

3

In accordance with the documentation until PHP7.x string conversion was affected by locale

A value can be converted to a string using the (string) $variavel or the function strval(). The string conversion is done automatically in the scope of an expression where a string is required. This happens when using the functions of echo or print, or when a variable is compared to a string. The sections on Types and Type Juggling make the following clearer.

A int or float is converted into a string that represents the number textually (including the exponent part for floats). Floating point numbers can be converted using exponential notation (4.1E+6).

In PHP 8, the decimal point character is always the "dot" (.). Before PHP 8, the decimal point character is defined in locale of the script (LC_NUMERIC), which is precisely what happened in your case.

In summary before PHP8 was affected by locale, to take the test I used the German_Germany.1252:

<?php

setlocale(LC_NUMERIC, 'German_Germany.1252');

// Apenas obtem o locale atual, sem alterar
$locale = setlocale(LC_ALL, 0);

echo 10.14;

echo "\n";

var_dump($locale);

In PHP7

10,14
999999999,14
string(99) "LC_COLLATE=C;LC_CTYPE=Portuguese_Brazil.1252;LC_MONETARY=C;LC_NUMERIC=German_Germany.1252;LC_TIME=C"

Output on PHP8:

10.14
string(78) "LC_COLLATE=C;LC_CTYPE=C;LC_MONETARY=C;LC_NUMERIC=German_Germany.1252;LC_TIME=C"

Possible causes of current output being comma:

  • You may be using a auto_prepend_include who are using setlocale()
  • The LC_NUMERIC (or LC_ALL) may have been configured in /etc/default/locale
  • Or it may even have been a setup on .bashrc

Check out these situations (Setenv Apache I could not test, maybe in Fast-CGI there is some configuration that affects the locale, If you find any details please update the reply).

  • Just for the record, in php.ini or intl.default_locale is used for functions Intl: https://www.php.net/manual/en/book.intl.php, which is not the case for string conversion.

Browser other questions tagged

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