javascript - (bignumber.js) "decimalPlaces" or "dp" not working - Stack Overflow

I'm creating a idle html5js game where I use bignumber.js library to be able to store arbitrarly

I'm creating a idle html5/js game where I use bignumber.js library to be able to store arbitrarly long numbers, as I requirement for a idle game. The problem is that when in the configuration I have the EXPONENTIAL_AT option setted to some number, and after the variable expoent is larger than EXPONENTIAL_AT, the decimalPlaces function don't work, it shows something like "2.93012393841298e+23" even with "largeNumber.decimalPlaces(4)".

Currently, part of the script is somewhat this:

BigNumber.config({
    DECIMAL_PLACES: 4,
    EXPONENTIAL_AT: 16,
    ROUNDING_MODE: BigNumber.ROUND_HALF_UP
});

var largeNumber = new BigNumber("10594.1931247123691823e+23");

console.log(largeNumber.dp(4).toString());

This shows in console: 1.05941931247123691823e+27, so the exponential notation works, but the rounding not.

I don't know what I'm doing wrong or if is a bug, but I would like that this should work as expected.

I'm creating a idle html5/js game where I use bignumber.js library to be able to store arbitrarly long numbers, as I requirement for a idle game. The problem is that when in the configuration I have the EXPONENTIAL_AT option setted to some number, and after the variable expoent is larger than EXPONENTIAL_AT, the decimalPlaces function don't work, it shows something like "2.93012393841298e+23" even with "largeNumber.decimalPlaces(4)".

Currently, part of the script is somewhat this:

BigNumber.config({
    DECIMAL_PLACES: 4,
    EXPONENTIAL_AT: 16,
    ROUNDING_MODE: BigNumber.ROUND_HALF_UP
});

var largeNumber = new BigNumber("10594.1931247123691823e+23");

console.log(largeNumber.dp(4).toString());

This shows in console: 1.05941931247123691823e+27, so the exponential notation works, but the rounding not.

I don't know what I'm doing wrong or if is a bug, but I would like that this should work as expected.

Share Improve this question edited Dec 27, 2021 at 15:06 Bergi 667k161 gold badges1k silver badges1.5k bronze badges asked Sep 28, 2020 at 15:21 ghsoaresghsoares 632 silver badges8 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

Remember that 10594.1931247123691823e+23 (and 1.05941931247123691823e+27) represent this number:

1059419312471236918230000000

As you can see, it doesn't have a fractional portion at all, there are zero significant digits to the right of the decimal point. So .dp(4) isn't going to affect it.

If you had a number with a fractional portion, .dp(4) would work. I've done an example below. For the purposes of the example, I've told BigNumber not to switch to exponential notation until 30 digits, so it's more obvious what's going on. But that's just to show what's going on, it's not a solution, you don't have to do that. There's no problem to solve: Your number is already rounded (in effect); it's a whole number.

Here's the example:

BigNumber.config({
    EXPONENTIAL_AT: 30,
});

// Just for reference:
const yourNumber = new BigNumber("10594.1931247123691823e+23");
console.log(yourNumber.toString());  // 1059419312471236918230000000

// Your number plus six places of fraction:
const num = BigNumber("1059419312471236918230000000.123456");
console.log(num.toString());         // 1059419312471236918230000000.123456

// Rounded:
const rounded = num.dp(4);
console.log(rounded.toString());     // 1059419312471236918230000000.1235
<script src="https://cdnjs.cloudflare./ajax/libs/bignumber.js/9.0.0/bignumber.min.js"></script>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745367273a4624640.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信