decimal.js icon indicating copy to clipboard operation
decimal.js copied to clipboard

Restore toFormat

Open clintonb opened this issue 4 months ago • 3 comments

The toFormat() method was removed a while ago in favor of a separate library, toFormat. Decimal.js feels incomplete without this method, and it seems strange to require a separate library for a single method. It's also unclear how to use this library with ESM. The developer ergonomics are just very odd.

@MikeMcl can you restore toFormat(), please?

clintonb avatar Aug 11 '25 22:08 clintonb

Speak your mind, why don't you? :grin:

Please tell me more about why it's unclear how to use this library with ESM.

I will look at toFormat presently.

MikeMcl avatar Aug 14 '25 11:08 MikeMcl

It's unclear because it's not documented, and naïvely changing the require() to an import won't work. I instead have to create a new class, which just seems wrong all to get one method that I honestly would expect to exist (although I haven't needed it until now, 3 years into my usage of the library).

import Decimal from 'decimal.js';
import toFormat from 'toformat';


// TS2632: Cannot assign to Decimal because it is an import.
Decimal = toFormat(Decimal);

const DecimalWithFormat = toFormat(Decimal);

const d = new DecimalWithFormat('1000.00');
console.log(d.toFormat(2));   // Prints 1,000.00

Subjectively: the developer ergonomics of requiring a new library that has to be configured in a non-ideal manner suck. That's me being honest about this specific situation, but still immensely grateful for the work that's been done. ❤️

clintonb avatar Aug 14 '25 16:08 clintonb

Related-- I had no idea toFormat existed. Could this be added to the FAQ section of the docs?

asselin avatar Sep 26 '25 13:09 asselin