FuelLabs / fuels-ts

Fuel Network Typescript SDK
https://docs.fuel.network/docs/fuels-ts/
Apache License 2.0
44.04k stars 1.35k forks source link

`bn` library bugs when you're using units / precision zero #3227

Closed LuizAsFight closed 1 month ago

LuizAsFight commented 1 month ago

fuels-ts SDK Version

latest

Toolchain Versions

not relevant

Node.js Version

No response

Browser

No response

Operating System

No response

Describe the Problem

when you call format({ units: 0, precision: 0 }) it will not return the correct value

the same happens on parseUnits function

Code Snippet

the workaround we've been doing on FE side is:

bn(amount).mul(10).format({ units: 1, precision: 0 });

which is basically multiplying by 10 first, then converting to units 1, which at the end of the day will be the same of passing units: 0

this is very overwhelming to do as we have many projects and some places can be forgotten, leading to number bugs.



### Contract ABI

_No response_

### Errors

_No response_
maschad commented 1 month ago

Thanks for opening this @LuizAsFight just so that we are on the same page, could you share a few examples of expected results. Such as:

 expect(bn('1000000000').format({ units: 0, precision: 0 })).toEqual('1,000,000,000');
 expect(bn('1000000000').format({ units: 0, precision: 2 })).toEqual('1,000,000,000.00');
     bn('1000000000').format({
        minPrecision: 2,
      })
    ).toEqual('1,000,000,000.00');
       bn('1000000000').format({
        minPrecision: 2,
        units: 8,
      })
    ).toEqual('10.00');

This will just help us to have a clear consensus around the desired output.