iriscouch / bigdecimal.js

Arbitrary-precision Javascript BigInteger and BigDecimal real numbers
Apache License 2.0
243 stars 25 forks source link

how do i set a math context? #1

Open boxxxie opened 12 years ago

boxxxie commented 12 years ago

it's from java.math, but i need to use it in the bigdecimal class.

var currencyContext = new bigdecimal.MathContext(2)
TypeError: Object 2 has no method 'gC'
boxxxie commented 12 years ago

enum contexts are giving me problems too.

var currencyContext = bigdecimal.MathContext.DECIMAL128;
undefined
bigdecimal.BigDecimal("123",currencyContext).toString()
java.lang.RuntimeException: Unknown call signature for obj = new java.math.BigDecimal: string function
jhs commented 12 years ago

Interesting. Which browser or Javascript engine are you using? Thanks!

boxxxie commented 12 years ago

Chrome. On ubuntu 11.10 I'll test on FireFox tomorrow. I found a work around and ill post it tomorrow.

fsateler commented 12 years ago

I'm having the same issue, node on linux

> process.versions
{ node: '0.6.16',
  v8: '3.8.9.16',
  ares: '1.7.5',
  uv: '0.6',
  openssl: '1.0.1b' }
chrismeyersfsu commented 10 years ago

Has anyone solution / workaround been found ?

MikeMcl commented 10 years ago

@chrismeyersfsu

Oddly, MathContext only seems to accept strings:

var x = new BigDecimal('5');
var y = new BigDecimal('3');   

var mc = new MathContext('precision=5 roundingMode=HALF_UP');
x.divide( y, mc );    // '1.6667'    

// A precision of 16 digits and rounding mode HALF_EVEN.
// Notice that DECIMAL64 is a function.
x.divide( y, MathContext.DECIMAL64() );    // 1.666666666666667    

// Two decimal places and rounding mode ROUND_HALF_UP.
x.divide( y, 2, 4 );    // '1.67'

// DOWN is a function, but ROUND_DOWN isn't.
x.divide( y, 6, RoundingMode.DOWN() );    // '1.666666'    
x.divide( y, 6, BigDecimal.ROUND_DOWN );    // '1.666666'