php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #65737 Unexpected decimal rounding
Submitted: 2013-09-22 21:32 UTC Modified: 2013-09-26 10:29 UTC
From: kyegil at gmail dot com Assigned:
Status: Not a bug Package: intl (PECL)
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
 [2013-09-22 21:32 UTC] kyegil at gmail dot com
Description:
------------
---
From manual page: http://www.php.net/numberformatter.formatcurrency#refsect1-numberformatter.formatcurrency-description
---
php NumberFormatter->formatCurrency() gives unexpected results when rounding decimals

Test script:
---------------
$fmt = new NumberFormatter( 'en_GB', NumberFormatter::CURRENCY );
echo $fmt->formatCurrency("0.005", "GBP")."\n";
echo $fmt->formatCurrency("0.015", "GBP")."\n";
echo $fmt->formatCurrency("0.025", "GBP")."\n";
echo $fmt->formatCurrency("0.035", "GBP")."\n";
echo $fmt->formatCurrency("0.045", "GBP")."\n";
echo $fmt->formatCurrency("0.055", "GBP")."\n";


Expected result:
----------------
£0.01
£0.02
£0.03
£0.04
£0.05
£0.06

Actual result:
--------------
£0.00
£0.02
£0.02
£0.04
£0.04
£0.06

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-09-25 22:24 UTC] aharvey@php.net
-Status: Open +Status: Not a bug
 [2013-09-25 22:24 UTC] aharvey@php.net
ICU uses banker's rounding by default. You can control the rounding mode via 
$fmt->setAttribute(NumberFormatter::ROUNDING_MODE, $mode), where $mode is one of 
the NumberFormatter::ROUND_* constants listed on 
http://php.net/manual/en/class.numberformatter.php#numberformatter.constant.round
-ceiling.
 [2013-09-26 10:29 UTC] kyegil at gmail dot com
I see. But setting Rounding Mode to Half Up still gives me exactly the same 
result (and so do all the other rounding modes):

$fmt = new NumberFormatter('en_GB', NumberFormatter::CURRENCY);
$fmt->setAttribute(NumberFormatter::ROUNDING_MODE, 
NumberFormatter::ROUND_HALFUP);
echo $fmt->formatCurrency("0.005", "GBP")."\n";
echo $fmt->formatCurrency("0.015", "GBP")."\n";
echo $fmt->formatCurrency("0.025", "GBP")."\n";
echo $fmt->formatCurrency("0.035", "GBP")."\n";
echo $fmt->formatCurrency("0.045", "GBP")."\n";
echo $fmt->formatCurrency("0.055", "GBP")."\n";
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Sat Apr 04 06:01:23 2020 UTC