Linked Questions

64 votes
6 answers
80k views

Java:Why should we use BigDecimal instead of Double in the real world? [duplicate]

When dealing with real world monetary values, I am advised to use BigDecimal instead of Double.But I have not got a convincing explanation except, "It is normally done that way". Can you please throw ...
Vinoth Kumar C M's user avatar
6 votes
2 answers
13k views

How to convert BigDecimal to float having 2 decimal in java [duplicate]

How can I convert a BigDecimal to float, having 2 decimal in java? BigDecimal x=new BigDecimal(any exponential term); Now I want to convert to float having 2 decimal point only, for example -0.45.
user8610600's user avatar
0 votes
2 answers
8k views

Should I use BigDecimal or double? [duplicate]

I have to calculate a fairly complex formula in my code, and I'm wondering how to decide whether I should use BigDecimal or just double to do it. The function is is: f(x) = 1.03^(4 - ((1/3) * (x-9)^...
jcm's user avatar
  • 5,579
0 votes
3 answers
2k views

Java double data type should never be used for precise values, such as currency? [duplicate]

When i review official oracle java tutorial about variables datatypes http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html I surprised when i read that Java double data type ...
Ahmed Nabil's user avatar
  • 18.2k
185 votes
24 answers
264k views

Retain precision with double in Java

public class doublePrecision { public static void main(String[] args) { double total = 0; total += 5.6; total += 5.8; System.out.println(total); } } The above ...
Deinumite's user avatar
  • 3,999
47 votes
4 answers
140k views

How to specify Double's precision on hibernate?

I try to create a table from hibernate annotations. I need to have a column of Double type, with the length specified like : (10,2). So SQL syntax show like: ... DOUBLE(10,2) .... I have tried to do ...
artaxerxe's user avatar
  • 6,361
45 votes
8 answers
10k views

A realistic example where using BigDecimal for currency is strictly better than using double

We know that using double for currency is error-prone and not recommended. However, I'm yet to see a realistic example, where BigDecimal works while double fails and can't be simply fixed by some ...
maaartinus's user avatar
  • 45.6k
11 votes
2 answers
1k views

Why was I asked to return an array for this interview task? [closed]

I am 2nd-year computer science student and I just got back from my first ever interview. Basically at the end my interviewer asked me to write a function that takes two arguments, an amount in pounds ...
StormzyInnit99's user avatar
0 votes
3 answers
17k views

Is double 0.0 is greater or less than 0 in java

I would like to ask for more clarification. Here my sample program double diff = 7.500 - 7.500; System.out.println(diff); // result 0.0 if (diff > 0) { System.out.println("+" + diff ); //...
Sai Ye Yan Naing Aye's user avatar
6 votes
4 answers
6k views

Which data type to use for manipulating currency

I am trying to decide which data type shall i use for a financial application. I have read that Double or BigDecimal should be used. And i am confused between them. Any help in this regard will be ...
Jabir's user avatar
  • 2,818
7 votes
2 answers
9k views

Spark decimal type precision loss

I'm doing some testing of spark decimal types for currency measures and am seeing some odd precision results when I set the scale and precision as shown below. I want to be sure that I won't have any ...
Jared's user avatar
  • 2,914
7 votes
6 answers
1k views

Printing Java doubles to System.out in Java [duplicate]

This code isn't working as I thought it would. a=-1; b=0.1; for(i=0;i<=20;i++){ System.out.println(i + ". x= " + a); a=a+b; } On the console I should see: 0. x= -1.0 ...
user3057683's user avatar
2 votes
1 answer
13k views

How to use ResultSet of java to get precise value of float of mysql

Is it possible in Java, I use ResultSet, to get the precise value of float of mysql? In my database of mysql, I have some values of float which are very long(for example, 123456789, more than 7 ...
martinwang1985's user avatar
0 votes
3 answers
630 views

What are these equivalent data types of C in Java?

Say I have very big numbers. I know in C we could declare: long long int // What is equivalent of this in Java? long double // and this? Thanks.
knd's user avatar
  • 1,682
1 vote
1 answer
1k views

Java DecimalFormat HALF_UP rounding error

I'm using the DecimalFormat with HALF_UP rounding mode and I have an escenery where is not working correctly and I don't know why. DecimalFormat df = new DecimalFormat("#.##"); df.setRoundingMode(...
j.barrio's user avatar
  • 1,026

15 30 50 per page
1
2 3 4 5