Skip to content

Commit

Permalink
Use decimal js
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben-Rey committed May 16, 2024
1 parent 8505568 commit f70b2af
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 27 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"dependencies": {
"@types/pbkdf2": "^3.1.2",
"big-varint": "^0.1.3",
"decimal.js": "^10.4.3",
"pbkdf2": "^3.1.2"
}
}
33 changes: 8 additions & 25 deletions packages/massa-web3/src/experimental/basicElements/mas.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { FIRST, FIVE, ONE, ZERO } from '../utils'
import Decimal from 'decimal.js'
import { FIRST, ONE } from '../utils'
import { U64, fromNumber } from './serializers/number/u64'

export const NB_DECIMALS = 9
Expand Down Expand Up @@ -104,7 +105,7 @@ export function fromString(value: string): Mas {
* Converts a Mas value to a string with rounding approximation.
*
* @param value - The Mas value.
* @param decimalPlaces - The number of decimal places to include in the string. If null, trailing zeros are removed.
* @param decimalPlaces - The number of decimal places to include in the string.
* @returns The value as a string.
*
* @throws An error if the value is too large to be represented by a U256.
Expand All @@ -113,33 +114,15 @@ export function toString(
value: Mas,
decimalPlaces: number | null = null
): string {
if (value >= BigInt(ONE) << BigInt(SIZE_U256_BIT)) {
if (BigInt(value) >= BigInt(ONE) << BigInt(SIZE_U256_BIT)) {
throw new Error(ERROR_VALUE_TOO_LARGE)
}

Check warning on line 119 in packages/massa-web3/src/experimental/basicElements/mas.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

const valueString = value.toString()
const integerPart = valueString.slice(ZERO, -NB_DECIMALS) || '0'
let decimalPart = valueString.slice(-NB_DECIMALS).padStart(NB_DECIMALS, '0')
const scaledValue = new Decimal(value.toString()).div(`1e+${NB_DECIMALS}`)

if (decimalPlaces === null) {
// If decimalPlaces is null, remove trailing zeros from the decimal part
decimalPart = decimalPart.replace(/0+$/, '')
} else if (decimalPlaces < NB_DECIMALS) {
// If decimalPlaces is specified and less than NB_DECIMALS, handle rounding
const roundingDigit = parseInt(decimalPart[decimalPlaces])

if (roundingDigit >= FIVE) {
// If the rounding digit is 5 or greater, round up
decimalPart = (
BigInt(decimalPart.slice(ZERO, decimalPlaces)) + BigInt(ONE)
)
.toString()
.padStart(decimalPlaces, '0')
} else {
// Otherwise, truncate the decimal part to the specified number of places
decimalPart = decimalPart.slice(ZERO, decimalPlaces)
}
if (decimalPlaces !== null) {
return scaledValue.toFixed(decimalPlaces, Decimal.ROUND_HALF_UP)
}

return decimalPart.length ? `${integerPart}.${decimalPart}` : integerPart
return scaledValue.toString()
}

0 comments on commit f70b2af

Please sign in to comment.