Giter Club home page Giter Club logo

Comments (2)

dart-github-bot avatar dart-github-bot commented on August 15, 2024 1

Labels: area-core-library, type-enhancement

Summary: The user proposes a new function truncateAsFixed() to truncate a double to a fixed number of decimal places without rounding, as toStringAsFixed() currently rounds the value. The user believes the rounding behavior of toStringAsFixed() is not sufficiently clear in the documentation.

from sdk.

lrhn avatar lrhn commented on August 15, 2024

I'm not sure that functionality is available in JavaScript, and it's something we'd have to fix manually.
It's also unclear what truncation should do, fx for the double value written as 10.43. That is, what string is 10.43.truncateAsFixed(2).
It looks like it should give "10.43", obviously, but the actual value for that double is slightly less than the rational number 10.43 (it's 10.42999999999999971578290569595992565155029296875 to be precise). What is printed by 10.43.toString() is itself rounded. So realistically, you'd probably see 10.43.truncateAsFixed(2) being "10.42". Quite surprising and probably not what you want.
So you do want rounding. You just want "the right rounding", which is actually quite hard to define.

The only way to win is not to play. Use integers for computations, then format them when you're done.

(Generally, it's just not recommended to use floating point numbers for money. Truncating may do what you want here, if defined in some way, or it may not, but since doubles can't correctly represent all multiples of 1/100, they're not necessarily well suited for anything where the precise value is important, and where rounding wrong can cost someone money. There are definitely places where using floating point numbers for money transactions is downright illegal.)

from sdk.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.