Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

toStringAsFixed and Number format does not work correctly #56068

Closed
KaushikGupta007 opened this issue Jun 24, 2024 · 2 comments
Closed

toStringAsFixed and Number format does not work correctly #56068

KaushikGupta007 opened this issue Jun 24, 2024 · 2 comments
Labels
closed-as-intended Closed as the reported issue is expected behavior

Comments

@KaushikGupta007
Copy link

Steps to reproduce

  1. Run code provided on any platform

Issue Description
In Flutter, the toStringAsFixed(2) method and NumberFormat.decimalPatternDigits(locale: "en_IN", decimalDigits: 2) can produce unexpected results when rounding certain numbers. Specifically, numbers with a third decimal place of 5 (e.g., 5.505, 5.515, 5.525, ..., 5.595) might not be rounded correctly, or speaking more generally any number ending with 5 in decimal places

Expected results

The correct results for rounding should be:

5.505 -> 5.51
5.515 -> 5.52
5.525 -> 5.53
5.535 -> 5.54
5.545 -> 5.55
5.555 -> 5.56
5.565 -> 5.57
5.575 -> 5.58
5.585 -> 5.59
5.595 -> 5.60

According to rule:
If the digit to the right is greater than 5, round up

Actual results

I have checked these results using

toStringAsFixed(2) method and
NumberFormat.decimalPatternDigits(locale: "en_IN", decimalDigits: 2)

  • 5.505 -> 5.50 (Wrong)
  • 5.515 -> 5.51 (Wrong)
  • 5.525 -> 5.53 (Correct)
  • 5.535 -> 5.54 (Correct)
  • 5.545 -> 5.54 (Wrong)
  • 5.555 -> 5.55 (Wrong)
  • 5.565 -> 5.57 (Correct)
  • 5.575 -> 5.58 (Correct)
  • 5.585 -> 5.58 (Wrong - Given by toStringAsFixed) -> 5.59 (Correct given by NumberFormat)
  • 5.595 -> 5.59 (Wrong)

Code sample

Code sample
import 'package:intl/intl.dart';

void main() {
  double number1 = 5.505;
  double number2 = 5.515;
  double number3 = 5.525;
  double number4 = 5.535;
  double number5 = 5.545;
  double number6 = 5.555;
  double number7 = 5.565;
  double number8 = 5.575;
  double number9 = 5.585;
  double number10 = 5.595;
  
  print(number1.toStringAsFixed(2));
  print(number2.toStringAsFixed(2));
  print(number3.toStringAsFixed(2));
  print(number4.toStringAsFixed(2));
  print(number5.toStringAsFixed(2));
  print(number6.toStringAsFixed(2));
  print(number7.toStringAsFixed(2));
  print(number8.toStringAsFixed(2));
  print(number9.toStringAsFixed(2));
  print(number10.toStringAsFixed(2));

  var format = NumberFormat.decimalPatternDigits(locale: "en_IN", decimalDigits: 2);
  
  print(format.format(number1));
  print(format.format(number2));
  print(format.format(number3));
  print(format.format(number4));
  print(format.format(number5));
  print(format.format(number6));
  print(format.format(number7));
  print(format.format(number8));
  print(format.format(number9));
  print(format.format(number10));
}

Screenshots or Video

Screenshots / Video demonstration

[Upload media here]

Logs

Logs
[Paste your logs here]

Flutter Doctor output

Doctor output
[√] Flutter (Channel stable, 3.22.2, on Microsoft Windows [Version 10.0.22621.3593], locale en-IN)
    • Flutter version 3.22.2 on channel stable at C:\Users\Admin\fvm\versions\3.22.2
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 761747bfc5 (12 days ago), 2024-06-05 22:15:13 +0200
    • Engine revision edd8546116
    • Dart version 3.4.3
    • DevTools version 2.34.3

[√] Windows Version (Installed version of Windows is version 10 or higher)

[√] Android toolchain - develop for Android devices (Android SDK version 33.0.1)
    • Android SDK at C:\Users\Admin\AppData\Local\Android\sdk
    • Platform android-34, build-tools 33.0.1
    • Java binary at: C:\Program Files\Android\Android Studio\jbr\bin\java
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-b2043.56-9586694)
    • All Android licenses accepted.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[√] Visual Studio - develop Windows apps (Visual Studio Community 2022 17.9.1)
    • Visual Studio at C:\Program Files\Microsoft Visual Studio\2022\Community
    • Visual Studio Community 2022 version 17.9.34616.47
    • Windows 10 SDK version 10.0.22621.0

[√] Android Studio (version 2022.2)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin can be installed from:
       https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
       https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-b2043.56-9586694)

[√] VS Code (version 1.89.1)
    • VS Code at C:\Users\Admin\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.90.0

[√] Connected device (5 available)
    • CPH2467 (mobile)                   • 1a715d74      • android-arm64  • Android 14 (API 34)
    • Android SDK built for x86 (mobile) • emulator-5554 • android-x86    • Android 11 (API 30) (emulator)
    • Windows (desktop)                  • windows       • windows-x64    • Microsoft Windows [Version 10.0.22621.3593]
    • Chrome (web)                       • chrome        • web-javascript • Google Chrome 126.0.6478.61
    • Edge (web)                         • edge          • web-javascript • Microsoft Edge 125.0.2535.85

[√] Network resources
    • All expected network resources are available.

• No issues found!
@dart-github-bot
Copy link
Collaborator

Summary: The toStringAsFixed(2) method and NumberFormat.decimalPatternDigits in Flutter fail to round numbers ending in 5 correctly, producing inconsistent results across different inputs. For example, 5.505 rounds to 5.50 instead of 5.51.

@dart-github-bot dart-github-bot added area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) labels Jun 24, 2024
@mraleph
Copy link
Member

mraleph commented Jun 24, 2024

That's how floating point numbers actually work. Floating point 5.505 is not the same as decimal number 5.505, so it absolutely correctly rounds to 5.5 if you ask less decimal digits.

I recommend:

  • Learning how floating point numbers work: https://floating-point-gui.de/
  • Never using floating point numbers for anything that requires correct decimal rounding (e.g. storing prices and monetary amounts).

@mraleph mraleph closed this as completed Jun 24, 2024
@mraleph mraleph added closed-as-intended Closed as the reported issue is expected behavior and removed area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. labels Jun 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-as-intended Closed as the reported issue is expected behavior
Projects
None yet
Development

No branches or pull requests

3 participants