php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #8097 printf() and float data vs.setlocale()
Submitted: 2000-12-04 13:08 UTC Modified: 2002-11-08 01:00 UTC
Votes:4
Avg. Score:4.5 ± 0.9
Reproduced:3 of 3 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: hh at loca dot net Assigned:
Status: No Feedback Package: Strings related
PHP Version: 4.0.3pl1 OS: Linux (SuSE 7.0)
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: hh at loca dot net
New email:
PHP Version: OS:

 

 [2000-12-04 13:08 UTC] hh at loca dot net
Changing the locale setting to a country that uses comma (,)
as the decimal seperator will stip off decimal places from
the float value in printf/sprintf. Example:

MySQL table:

	create table test (
	    name varchar(100),
	    value float(16,2)
	);

	insert into test values ('bla', 3.789);

PHP code:

	setlocale("LC_ALL", "de_DE");
	// mysql_connect etc.
	$row = mysql_fetch_array($result);
	printf("%s: %.2f",
	    $row["name"], $row["value"]);

Result:

	bla: 3.00

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-07-31 16:14 UTC] andy@php.net
unable to reproduce with latest version of PHP, please
upgrade and re-open if still a problem.
 [2002-10-23 06:53 UTC] kaatz at wwe dot biz
This problem also exits in our application which runs on php-4.2.3.
We use 'setlocale' there to handle gettext translations. With LC_ALL set by 'setlocale' all prices in the shopping basket and VAT calculations with floats are 'calculated wrong'

Thanks for that hint.
Juergen
 [2002-10-23 07:04 UTC] hholzgra@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php4-latest.tar.gz
 
For Windows:
 
  http://snaps.php.net/win32/php4-win32-latest.zip


 [2002-11-08 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over 2 weeks, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 [2006-11-29 09:30 UTC] essen at dev-extend dot eu
Version: 5.1.6

Linux 2.4. Don't know exactly which Linux, it's the one on 1&1.
I encountered this bug on 5.1.6 but I can't test on 5.2.
Since this bug seems old and has been closed all this time I guess it isn't fixed yet.
It works probably on all locales enabled systems. But maybe not.

I won't be able to test it because I don't have the  LC_NUMERIC locales and don't really know how to install them on NetBSD.

However I can help you reproduce this bug.

You need to use a locale from a country that uses comma (,)
as the decimal seperator, as said hh before. You must have all the locale category installed (but LC_NUMERIC is probably the only one really needed). You only have to do this:

<?php

setlocale(LC_ALL, 'fr_FR'); // or 'de_DE' or others, too
printf('%.2f', 42.4242);

?>

This will print "42.00".
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Oct 27 16:01:27 2024 UTC