0
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 i m getting error as "Uncaught ReferenceError: Double is not defined" In browser console.

Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162
Nandhu
  • 3
  • 2
  • 4
  • 4
    JavaScript has no concept of big decimals in the language itself, all numbers are represented as doubles. – Qantas 94 Heavy Jan 26 '15 at 10:02
  • Are you using some library for big decimals? Or are you talking about a Java `BigDecimal` that you want to somehow bring into a JavaScript program? – joews Jan 26 '15 at 10:08
  • @Quantas 94 Heavy, to be precise, actually, in Javascript there is not even a `double` data type, every numeric values are of type `Number`, `Number, Boolean, String, Null, Undefined` are the primitives in Javascript and then `Object` – Trash Can Jan 26 '15 at 10:11
  • 1
    @Dummy: the JavaScript `Number` type is in fact a double, sorry for the confusion. – Qantas 94 Heavy Jan 26 '15 at 13:38
  • Tx all for your response – Nandhu Jan 28 '15 at 11:51

2 Answers2

0

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

alexey28
  • 5,170
  • 1
  • 20
  • 25
0
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
Code
  • 679
  • 5
  • 9