Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Simple decimal arithmetic for the browser and node.js!

License

NotificationsYou must be signed in to change notification settings

github1586/decimal-js

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

46 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Simple decimal arithmetic for the browserand node.js!

Why?

Why don’t my numbers, like 0.1 + 0.2 add up to a nice round 0.3,and instead I get a weird result like 0.30000000000000004?Because internally, computers use a format (binary floating-point)that cannot accurately represent a number like 0.1, 0.2 or 0.3 at all.

Source :http://floating-point-gui.de/

I wrote this because I needed to do simple computation in the browserand I couldn't find a lightweight library to do it.

How to use?

In the browser

<script src="lib/decimal.js"></script>

In node

npm install decimal

then in your program

var Decimal = require('decimal');

Examples

>>> 1.1 + 2.23.3000000000000003>>> Decimal('1.1').add('2.2').toNumber()3.3>>> 4.01 * 2.018.060099999999998>>> Decimal('4.01').mul('2.01').toNumber()8.0601>>> Decimal.mul('4.01', '2.01').toNumber()8.0601

Can I help?

Of course you can, I suck at math, and this implementation is very naive.If you are a math Guru and you see something wrong or away to simplify things you can send in a pull request.

Methods

Decimal(n)

Create a newDecimal fromn.n can be a string, integer, oranotherDecimal.

.toString()

Returns theDecimal instance as a string.

.toNumber()

Turn aDecimal into aNumber.

.add(n)

Return a newDecimal containing the instance value plusn.

.sub(n)

Return a newDecimal containing the instance value minusn.

.mul(n)

Return a newDecimal containing the instance value multiplied byn.

.div(n)

Return a newDecimal containing the instance value integrally divided byn.

About

Simple decimal arithmetic for the browser and node.js!

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript93.9%
  • HTML6.1%

[8]ページ先頭

©2009-2025 Movatter.jp