Questions tagged [mathcontext]

12 questions
63
votes
5 answers

Use of java.math.MathContext

Recently I tried understanding the use of java.math.MathContext but failed to understand properly. Is it used for rounding in java.math.BigDecimal. If yes why does not it round the decimal digits but even mantissa part. From API docs, I came to know…
jatanp
  • 3,982
  • 4
  • 40
  • 46
3
votes
2 answers

Mathcontext needed for add, subtract and multiply?

first of, my search skills may be not as good as I hoped, so maybe this kind of question exists already. If so please tell me.. See this code below: new BigDecimal("5").add(new BigDecimal("7")); vs new BigDecimal("5").add(new BigDecimal("7"),…
BAERUS
  • 4,009
  • 3
  • 24
  • 39
3
votes
1 answer

inconsistent behavior from BigDecimal and MathContext

I am Seeing some strange behavior from BigDecimal When I do division using mathContext the output is different than when I do the division by directly providing the scale and rounding mode Here is an example that I think should provide the same…
thermite
  • 502
  • 6
  • 19
3
votes
0 answers

MathContexts in BigDecimals - ScalaCheck generator creates BigDecimals which can't be serialized then deserialized. How to use MathContexts correctly?

I discovered an issue in Scalacheck whereby arbitrary[BigDecimal] would generate BigDecimals which could not be converted to Strings and then back into BigDecimals, and I'm trying to work with the creator to find a fix for it, but I'm unsure of how…
2rs2ts
  • 10,662
  • 10
  • 51
  • 95
1
vote
0 answers

What is the purpose of the MathContext parameter in the methods of BigDecimal class?

The BigDecimal class has the following arithmetic and rounding methods: BigDecimal add (BigDecimal y) BigDecimal subtract(BigDecimal y) BigDecimal multiply(BigDecimal y) BigDecimal divide (BigDecimal y, [int scale,] RoundingMode rm) BigDecimal…
iwis
  • 1,382
  • 1
  • 13
  • 26
1
vote
1 answer

IBM java8 BigDecimal floating issue with MathContext.DECIMAL64

BigDecimal in IBM java 8 calculates incorrect value when adding floating decimal number with MathContext.DECIMAL64. Below is example code MathContext context = MathContext.DECIMAL64; BigDecimal p = BigDecimal.valueOf(0.05000); BigDecimal test =…
Hai Quach
  • 7
  • 5
1
vote
1 answer

MathContext.DECIMAL32 vs MathContext.DECIMAL64, which one to use, and why?

Should I use MathContext.DECIMAL32 or MathContext.DECIMAL64? I have looked at the documentation, but I couldn't really understand when to use either. I'm using BigDecimal to represent a percentage that I want to apply to an amount of money.…
has981
  • 425
  • 4
  • 18
1
vote
1 answer

How to replace all instances of a substring but only whole worlds in JS?

For example this function function replaceAll(str,x,y){ return str.split(x).join(y); } var x="My cat have a hat a its head."; replaceAll(x,"at","on") will return from this My cat have a hat at its head. to this My con have a hon on its…
DBalazs
  • 133
  • 2
  • 9
0
votes
1 answer

How to get the exact value in BigDecimal?

I want to get a BigDecimal value with the following attributes: Rounding mode: Halfe-even number of digits after the point: 2 I have the following code: public BigDecimal standardDeviation() { MathContext mc = new MathContext…
0
votes
1 answer

Is there any condition where Bigdecimal with MathContext.DECIMAL32 does not provides correct result?

System.out.println("Result="+new BigDecimal(((63.19* 15) + (63.37* 5))).divide(new BigDecimal(15 + 5), MathContext.DECIMAL64).doubleValue()); Result=63.23499999999999 But with MathContext.DECIMAL32 we are getting correct result, see…
A_Coder
  • 3
  • 3
-1
votes
2 answers

How to set scale for Math.Context in Java?

I'm using Math.Context in my coding. Now the problem is Math.Context is calculating everything after decimal. I want to define Scale of 8 (means 8 digits after decimal point) for Math.Context. Please note i'm not looking for precision. I know how to…
user13986339
-2
votes
2 answers

golden ratio calculation with precision

I've got the task to calculate the golden ratio (phi = (1+ sqrt(5))/2). But I need to calculate it with about 50 decimal digits / decimal places and then round the result up to 30 decimal digits and print it on the console. I am allowed to use…
user12438414