Skip to content

Commit

Permalink
Fixes #3500 (#3846)
Browse files Browse the repository at this point in the history
  • Loading branch information
ncave authored Jun 13, 2024
1 parent c0b1bce commit 6ae8390
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/Fable.Cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

### Fixed

* [JS/TS] Fixed BigInt.ToDecimal with negative values (#3500) (by @ncave)

## 4.19.1 - 2024-06-13

### Fixed
Expand Down
7 changes: 4 additions & 3 deletions src/fable-library-ts/BigInt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,11 @@ export function toFloat32(x: bigint): float32 { return Number(x); }
export function toFloat64(x: bigint): float64 { return Number(x); }

export function toDecimal(x: bigint): decimal {
const low = Number(BigInt.asUintN(32, x))
const mid = Number(BigInt.asUintN(32, x >> 32n))
const high = Number(BigInt.asUintN(32, x >> 64n))
const isNegative = x < zero;
const bits = abs(x);
const low = Number(BigInt.asUintN(32, bits));
const mid = Number(BigInt.asUintN(32, bits >> 32n));
const high = Number(BigInt.asUintN(32, bits >> 64n));
const scale = 0;
return fromParts(low, mid, high, isNegative, scale)
}
Expand Down
8 changes: 8 additions & 0 deletions tests/Js/Main/ConvertTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1090,6 +1090,14 @@ let tests =
let value = 1.0m
decimal (bigint value) |> equal value

testCase "BigInt ToDecimal with Decimal.MinValue works" <| fun () ->
let value = Decimal.MinValue
decimal (bigint value) |> equal value

testCase "BigInt ToDecimal with Decimal.MaxValue works" <| fun () ->
let value = Decimal.MaxValue
decimal (bigint value) |> equal value

testCase "BigInt ToString works" <| fun () ->
let value = 1234567890
string (bigint value) |> equal "1234567890"
Expand Down
10 changes: 10 additions & 0 deletions tests/Python/TestConvert.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1188,6 +1188,16 @@ let ``test BigInt ToDecimal works`` () =
let value = 1.0m
decimal (bigint value) |> equal value

[<Fact>]
let ``test BigInt ToDecimal with Decimal.MinValue works`` () =
let value = Decimal.MinValue
decimal (bigint value) |> equal value

[<Fact>]
let ``test BigInt ToDecimal with Decimal.MaxValue works`` () =
let value = Decimal.MaxValue
decimal (bigint value) |> equal value

[<Fact>]
let ``test BigInt ToString works`` () =
let value = 1234567890
Expand Down
10 changes: 10 additions & 0 deletions tests/Rust/tests/src/ConvertTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1257,6 +1257,16 @@ let ``BigInt ToDecimal works`` () =
let value = 1.0m
decimal (bigint value) |> equal value

[<Fact>]
let ``BigInt ToDecimal with Decimal.MinValue works`` () =
let value = Decimal.MinValue
decimal (bigint value) |> equal value

[<Fact>]
let ``BigInt ToDecimal with Decimal.MaxValue works`` () =
let value = Decimal.MaxValue
decimal (bigint value) |> equal value

[<Fact>]
let ``BigInt ToString works`` () =
let value = 1234567890
Expand Down

0 comments on commit 6ae8390

Please sign in to comment.