Hi all,

I have a web app that is used in a number of countries, including ones such as Brazil, where the comma is used as a decimal separator. This is causing problems with my SQL queries, as it is trying to update a number field with "0,013" when it should be "0.013". The result is simply "0".

I set the internationalization environment variables with:

setlocale( LC_ALL, "ptg");	
putenv("LC_ALL=pt_BR);	
putenv("TZ=America/Sao_Paulo);

When it comes to the dots and commas, I use a simple number_format() to display it to the user as they would expect. But unfortunately, once the local is set, any calculations done by PHP result in a comma-separated value.

eg:

$numval = 0.0155 - 0.0133;
echo $numval;

Returns "0,0122".

Is it possible to still set the locale as above, but force PHP to use the period decimal separator?

Formatting each value before writing to the database is the last option I'd like to consider.

Thanks in advance for any help.

Recommended Answers

All 2 Replies

number_format( $string , 2 , "Decimals" , "Thousands" );
function fomat_brazil ( $case ) {
// Presuming the brazilian use "," as the equvilant of "." or the reverse
echo number_format( $case , 2 , "," , "." );
}

function format_english( $case ) {
echo number_format( $case , 2 , "." , "," );
}

Hope this helps ;)

Hey, thanks for the reply. Formatting each number like that would be the last resort for me, as it would require a lot of code changes.

I think I'd solved the issue however, by being more selective with the locale settings rather than setting "LC_ALL". I now have:

setlocale( LC_NUMERIC, 'en_GB');
setlocale( LC_COLLATE, 'ptg');
setlocale( LC_CTYPE, 'ptg');
setlocale( LC_MONETARY, 'ptg');
setlocale( LC_TIME, 'ptg');

This leaves the other locale settings as they should be, but forces the decimal separator to the British standard.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.