Comments (6)
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:
- Utilize
Number(new Decimal(...).toFixed(2));
, which feels overly cumbersome due to its nested structure. - Employ
new Decimal(...).toDP(2)
, which unfortunately results in a string being sent to the client. - 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.
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.
@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.
@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.
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.
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)
- Return type of sign() is Decimal in d.ts HOT 3
- some cases normal operations still accurate than decimal.js HOT 4
- jest toEqual doesn't work HOT 1
- mathjs and decimal.js output different results HOT 1
- Allow creation without the `new` keyword HOT 4
- isPositive() should return false for zero HOT 1
- Maximum call stack size exceeded HOT 3
- Precision is lost when working with cyclic division HOT 1
- Round Half Up working as expected? HOT 1
- toFixed() is inconsistent in some way HOT 3
- Add structuredClone support HOT 3
- Deviding by 3 HOT 3
- Create a security policy HOT 1
- Assigning to constructor at runtime
- Support for mathematical expression processing without loss of precision HOT 2
- Decimal.sum with large arrays exceeds the maximum call stack size
- Parsing from binary (Python encoding via avro) HOT 1
- question:Value of type 'typeof Decimal' is not callable. Did you mean to include 'new'? HOT 2
- Irregular length of precision for at least .sub method HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from decimal.js.