Coder Social home page Coder Social logo

Comments (6)

AbdelrahmanHafez avatar AbdelrahmanHafez commented on June 3, 2024 1

I'm opting to leave this issue open for the time being. The toDecimalPlaces(...) method does alleviate the problem to an extent, as it spares us from adding an additional level of indentation. However, it also introduces a new challenge.

The method returns a Decimal object, which is logical given its functionality. Yet, this becomes problematic when I primarily use toDecimalPlaces(...) in the context of preparing data for JSON API responses. These responses are subjected to JSON.stringify(...), which, when encountering a non-primitive type like Decimal, defaults to invoking the .toString() method. Consequently, the value is transformed into a string before reaching the client.

At present, my alternatives are somewhat limited:

  1. Utilize Number(new Decimal(...).toFixed(2));, which feels overly cumbersome due to its nested structure.
  2. Employ new Decimal(...).toDP(2), which unfortunately results in a string being sent to the client.
  3. Opt for new Decimal(...).toDP(2).toNumber(), which, despite being a two-step process, seems like a more palatable option than the first.

A more streamlined solution, such as new Decimal(...).toDPN(2) (short for toDecimalPlacesNumber), would be highly beneficial for enhancing conciseness. However, I acknowledge that this suggestion might deviate from the library's established design principle of methods returning a Decimal object, which could potentially introduce breaking changes.

from decimal.js.

AbdelrahmanHafez avatar AbdelrahmanHafez commented on June 3, 2024

Just found out about toDecimalPlaces which has an alias toDP after nearly a decade of frustration of having to unnecessarily add another level of indentation to my code.

from decimal.js.

calebergh avatar calebergh commented on June 3, 2024

@AbdelrahmanHafez - This idea, unfortunately, won't work. Javascript won't preserve decimal places in numbers - which is why the existing methods return strings. You can test this yourself: console.log(1.20) will output 1.2, console.log(1.00) will output 1. To get fixed decimal places, you must generate a string. It's just the way Javascript handles number.

While it would be great if we could get this, it's impossible. This issue should be closed.

from decimal.js.

AbdelrahmanHafez avatar AbdelrahmanHafez commented on June 3, 2024

@calebergh
I think there is a misunderstanding. I am aware that 1.20 is the same as 1.2 in JavaScript, and that is perfectly fine.

What I'm proposing is an alias to decimal.toDP(2).toNumber() without any difference in the behavior, the comments above is me explaining why I need it, it's a very common use-case in my experience, and there is currently no concise way to achieve such a common functionality.

from decimal.js.

calebergh avatar calebergh commented on June 3, 2024

Ah. I see. It's clarified in your later comments.

Seems to me a small utility function that does this for you would provide you with the output you want, and still be very simple to use...

const toDecimal = (num: Decimal): number => {
    return num.toDP(2).toNumber();
}

And just use toDecimal wherever it's needed.

It's my guess the requested feature won't get added, because it's pretty trivial to do. But I'm curious what the package author thinks :)

from decimal.js.

MikeMcl avatar MikeMcl commented on June 3, 2024

Thanks for the suggestion @AbdelrahmanHafez. Although I can see that adding such a method could be useful for some users, there is a high bar to further additions to the API that is not quite met here.

from decimal.js.

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.