Skip to content

Commit

Permalink
Fix toBigInt function
Browse files Browse the repository at this point in the history
  • Loading branch information
Neylix committed Oct 27, 2023
1 parent b05f96d commit 6f5d65a
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,14 @@ export function uint8ArrayToInt(bytes: Uint8Array): number {
* @param decimal Number of decimals
*/
export function toBigInt(number: number, decimal: number = 8): number {
return Math.trunc(number * Math.pow(10, decimal));
// This is a workaroud of float weird behavior
// 94.03999999999999 * 100_000_000 = 9404000000
// 94.03999999999999 * 10*10*10*10*10*10*10*10 = 9403999999
let nb = number
for (let i = 0; i < decimal; i++) {
nb = nb * 10
}
return Math.trunc(nb)
}

/**
Expand Down Expand Up @@ -261,4 +268,4 @@ export function serializeString(str: string): Uint8Array {
*/
export function deserializeString(encoded_str: Uint8Array): string {
return new TextDecoder().decode(encoded_str)
}
}

0 comments on commit 6f5d65a

Please sign in to comment.