简体   繁体   中英

how to convert big decimal value to decimal in javascript

var zx =1800;

I have some value in big decimal value i need to convert it as decimal in javascript.

I have tried using zx.doubleValue() method but im getting error as "Uncaught ReferenceError: Double is not defined" In browser console.

As I understood you receiving big decimal from backend. The problem with JavaScript is that native Number type is limited in precision, thus your big decimal will be truncated.

You can use one of libraries, for example, big number: https://www.npmjs.com/package/big-numbers

This will allow you to convert received from backend numbers to big number:

const bigNumberValue = numbers.of([received from backend value]);

After it is done, you can perform any required operation: add, multiply, etc. Check JavaScript tutorial here:

http://bignumbers.tech/tutorials/java-script

var bigdecimal = require("bigdecimal");

var i = new bigdecimal.BigInteger("1234567890abcdefghijklmn", 24);
console.log("i is " + i);
// Output: i is 60509751690538858612029415201127

var d = new bigdecimal.BigDecimal(i);
var x = new bigdecimal.BigDecimal("123456.123456789012345678901234567890");
console.log("d * x = " + d.multiply(x));
// Output: d * x = 7470299375046812977089832214047022056.555930270554343863089286012030

var two = new bigdecimal.BigDecimal('2');
console.log("Average = " + d.add(x).divide(two));
// Output: Average = 30254875845269429306014707662291.561728394506172839450617283945

var down = bigdecimal.RoundingMode.DOWN();
console.log("d / x (25 decimal places) = " + d.divide(x, 25, DOWN));
// Output: d / x (25 decimal places) = 490131635404200348624039911.8662623025579331926181155

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM