What can I use as a decimal type in JavaScript? It's not supported (`0.1 + 0.2 !== 0.3`

), and I need it for representing exact values in a banking/financial application. See *The State and Future of JavaScript* for a good read and the dirty details behind JavaScript and it's (lack of) support for decimal arithmetic.

By "decimal", I mean either:

- infinite-range and arbitrary-precision (like
`BigDecimal`

in Java), or - limited range and precision, but suitable for financial calculations (like
`decimal`

in C#).

So, what library or solution exists for working with decimal values? Thanks!

Javascript does have floating point support. But anyway, for financial records, the simplest implementation would simply be storing your values in standard integers. You may either declare an integer to represent the amount in cents, or two integers one for dollars and one for cents.

So $18.57 would become 1857cents in the first technique or 18 dollars and 57 cents in the second.

This has the added advantage of being completely accurate, as integers are stored purely as unique binary representation, there would be no such thing as rounding errors.

It is often recommended^{1} to handle money as an integer representing the number of cents: `2572`

cents instead of `25.72`

dollars. This is to avoid the problems with floating-point arithmetic that you mention. Fortunately integer arithmetic in floating point is exact, so decimal representation errors can be avoided by scaling.

^{1}Douglas Crockford: JavaScript: The Good Parts: Appendix A - Awful Parts (page 105).

It seems the following library implements decimal in js (node and browser): https://npmjs.org/package/jsdecimal

Similar Questions

how I should avoid decimal rounding in javascript suppose I have input like input : 99999999999.999999 Expected output : 99999999999.999999 but i am getting output like 100000000000 here, interna

I have a binary array with hex values and i would like to convert it into decimal. once that is done i would like to display it in a string. BTW i am working on an intel process via unbuntu and this w

In dijit/form/HorizontalSlider on change I am getting values in a textbox. The values are decimal, like 51.66777777. I want only 51. <div id=horizontalSlider></div> <input type=text

My controller queries the database and organizes some values. I need to pass these values to javascript(JQuery) which use the values to fullfill UI features. How to pass values from controller to java

I have a problem currently where a system we are connecting to expects to receive XML which contains, among other things, three double fields formatted to one decimal place. Personally I feel that our

im having trouble returning an array that contains decimal values to a variable: function foo() { var coords = new Array(39.3,18.2); console.log(coords[1]); //successfully logs 18.2 return coords; }

Why do my decimal value rows are values such as #> instead of just a regular decimal number? I have a cost column and I get weird values. Is that ok? If so why?

I am working with EXT-JS and am using a VType to specify the regex for a particular field in a form. I need this field to accept values like: 1, 11, 11561.464, 1.0, 56.0, etc and reject values like: .

I'm trying to figure out decimal data type of a column in the SQL Server. I need to be able to store values like 15.5, 26.9, 24.7, 9.8, etc I assigned decimal(18, 0) to the column data type but this n

I have written this code Qty = 5 Rate = 50.10 SELECT cast(convert(decimal, qty)+ convert(decimal, rate) as decimal) FROM product where code='P0001' Result = 250 it should be 250.50

How do I convert a string to a decimal in unity javascript?

I am trying to assign some javascript variables in a Django template. I am having a problem where the values I am assigning are being written to the page properly (I can see them in the page source),

It's a pretty simple question to be honest. I've been looking for a while now on Google for a solution but nothing seems to work. I have the following field in my database: decimal(2,1) I have two va

I have a datagridview that uses AutoGenerateColumns in winforms. There is a date column and then 1-16 data columns containing numeric values. I want all of these 1-16 data columns to be formatted to 4

I am trying to set decimal values,Below is my input string String rate=1.000000000; Converting to double: Double converted=Double.valueOf(rate); DecimalFormat format=new DecimalFormat(#.########)

I am trying to limit an input to a decimal number. I'd like any invalid characters not to be displayed at all (not displayed and then removed). I already have this implemented but for whole integers (

My fields are tax rate , tax amount. I had written this validation :format => { :with => /\A[+]?\d+\Z/}. But it is not taking values e.g., 4.67. It is giving error in it What validation shall I

Im very new html, javascript, and css so please forgive if my question sounds idiotic to you. My question is how can I prevent the function toFixed() from rounding of the decimal number. Here's my lin

For an example, assume an initial value of: 0100 0011 What is this in hex and what is it in decimal if using two's complement? Similarly, what is it in hex and decimal if using one's complement? More

I am trying to deal with JavaScript values such as 23.45, but I want to be able to do mathematical operations on these values (addition, subtraction, multiplication, division) without running into flo

In SQL Server(T-SQL) you can convert a DateTime variable to a a decimal values like this: CONVERT(DECIMAL(20,10),@mytime) Sample Input: 2012-07-27 08:29:20.000 Sample Output: 41115.3537037037 Is ther

I am trying to create a Regular Expression Validation that allows a) Decimal numbers up to 3 decimal places which are greater than zero b) Either positive or negative but non-zero numbers up to 3 dec

I'm trying to add a decimal value to a 10 bit std_logic_vector without having to describe every bit. Though it might not be worth the trouble in this particular scenario, i believe it will be very goo

Is there any practical difference between the .net decimal values 1m and 1.0000m? The internal storage is different: 1m : 0x00000001 0x00000000 0x00000000 0x00000000 1.0000m : 0x000186a0 0x00000000 0x

I need to round off the decimal value to decimal places using javascript. Ex,: 16.181 to 16.18 16.184 to 16.18 16.185 to 16.19 16.187 to 16.19 I have found some answers, but most of them do not roun

I'm completely new to rails and have a bit of a question about some decimal values from my database showing up as being slightly off by a very small amount in the default scaffolding index/show output

I have a number with decimal places and I am wondering if it's possible to round the decimal to the nearest whole using javascript? My number is: 4.59 I need my number to round to: 4.60

I need to round up to 1 decimal place If my number is 80.02 I need it to be 80.1. tried Math.ceil( (80.02).toFixed(1) ) but this rounds the number up to 81 How can I achieve this?

I have the following javascript code to add checkboxes together when selected and produce a total. What do I need to add to the code to get the total to display 2 decimal places always. <script typ

how to restrict user to enter only numbers with 6 digits and two decimal values in textbox using javascript?? //Function to allow only numbers to textbox function validate(key) { //getting key code of

I wanted to create a Javascript regex, which accepts 123, 123.123, 12.34, 1.324 But should not accept 1246, 1234.45, 1.2364

Is There a way to do an hash map in javascript that holds 1 key and 2 values? For an hash map with key and value in javascript i can do something like this: var userList = { 11234321:koko, 22342342:

I have always been coding in java and have recently started coding in javascript (node.js to be precise). One thing that's driving me crazy is add operation on decimal numbers; Consider the following

I'm trying figure out how to compare decimal values in PHP properly. $get_decimal_value = mysqli_query($conn, SELECT DecimalValue FROM my_table WHERE Id = $some_id LIMIT 1) or die($db_conn_err); if(

I have an odd bug in one of my applications. When I am using the sqlite3 database the bug is not present. However when I use mysql2 as the database adapter I run into an error saving decimal values f

I have a javascript code that compares two values: } else if (!(parseInt($('#form_value').val()) >= 1)){ alert(Error: You didn't pick a number!); form_value in this case is 001, and I would like

The below routine replace activecell integer numbers with the number and = before the number and the number two columns to the right, but display error 1004 when the number is decimal. How can I fix i

I have set my MySQL field table data type to the decimal because from what I have read, I would be able to store the price with commas/dots in the decimal data type fields... The problem is that whene

How to make the Textbox field accept only Decimal Values

how to insert values in a html table, an array multidimensional in javascript. Additionally, such as performing calculations between the columns of the array and insert the results into a column of th

This question already has an answer here: How can I get query string values in JavaScript? 74 answers I have a form that uses the get method and contains an array: http://www.example.com?name[]

In javascript how the hexadecimal and octal integers are converted to the decimal values: For example: var num=parseInt(0x10); document.write(num); It displays as output the value 16 how??

I have a decimal variable which represents a donation amount. Currently I am displaying it on screen as a currency like so- DonationAmount.ToString(C); This gives the following output (given a US l

I have a number of values stored as varchar, where values are stored in the string form: 10,000,000.00 I would like to convert this number to a double to display: 10000000.00 AND then SUM it with the

I just read on MDN that one of the quirks of JS's handling of numbers due to everything being double-precision 64-bit format IEEE 754 values is that when you do something like .2 + .1 you get 0.3000

Sybase ODBC driver have an issue with the decimal data type. For example, when an application is trying to save in the database a decimal value occurs this error: ERROR [22018] [DataDirect][ODBC Syba

I have been using ORM tools/code generators and I see that they prefer using decimal to int values when mapping columns to properties. Is there any advantage of using decimal ? The column type in data

I have a custom modal class which contains a decimal member and a view to accept entry for this class. Everything worked well till I added javascripts to format the number inside input control. The fo

This may be a easy question but i am not able to find the logic. I am getting the values like this 12.010000 12.526000 12.000000 12.500000 If i get the value 12.010000 I have to display 12.01 If i ge

converter_scientific_notation_to_decimal_notation('1.34E-15') or converter_scientific_notation_to_decimal_notation(1.34E-15) => '0.00000000000000134' converter_scientific_notation_to_decimal_notati