Home > Net >  Java Double to String
Java Double to String

Time:11-16

In java ı want a method when a user enters double number money value (for example 317.93) the program should display the number like this: 1---200 1---100 0---50 0---20 1---10 1---5 2---1 1---0.5 1---0.25 1---0.1 1---0.05 3---0.001(first number how many are there and second numbers what are the banknotes)

Scanner input = new Scanner(System.in);
double o = input.nextDouble();
int a =(int)o;
int m=a;
int x=a/200;
a=a-(a/200*200);
int y=a/100;
a=a-(a/100*100);
int z=a/50;
a=a-(a/50*50);
int w=a/20;
a=a-(a/20*20);
int q=a/10;
a=a-(a/10*10);
int d=a/5;
a=a-(a/5*5);
int c=a/1;
double k=o-(double)m;//for getting fraction part
double n=k/0.5;
k=k-0.5;
double e=k/0.25;
k=k-(0.25);
double l=k/0.10;
k=k-((int)l*0.1); //ıwas going to continue but here number becomes 0.99999964 instead of 0.1

System.out.println(x " - " "200");
System.out.println(y " - " "100");
System.out.println(z " - " "50");
System.out.println(w " - " "20");
System.out.println(q " - " "10");
System.out.println(d " - " "5");
System.out.println(c " - " "1");
System.out.println((int)n " - " "0.5");
System.out.println((int)e " - " "0.25");
System.out.println((int)l " - " "0.10");

ı wrote this but when number comes to fraction part 0.1 becomes 0.999999 so ı am getting a result ı did not want to.Also this method so long)

Any idea how can correct this method or another methods? Thanks in advance.

CodePudding user response:

The problem with doubles is that they do silent rounding. There are slightly less than 2^64 numbers that a double can represent. Given that double covers everything from -infinity to infinity, and there are an infinite numbers between 0.000 and 1.000, that should make you wonder: Wait, if double can represent an infinite infinity, and yet also fits in 64-bit, how does that work?

It 'works' by actually only representing 2^64 distinct specific numbers. Let's call those 'the blessed numbers', and any and all math you do on doubles will silently round to the nearest blessed number. There is no opportunity to know how much error this introduces (how could it; that error value probably isn't blessed either!)

The conclusion is: double means you are dead in the water here.

You don't want em.

The fix is: You have specific atomary units here, as is the case for every currency: There's no such thing as half a dollarcent, no such thing as half a yen, no such thing as half a satoshi: All relevant systems (such as banks, the bitcoin blockchain, etc) simply are incapable of representing anything except whole units of this atomary concept.

So, use that. Use a long that represents how many atoms you have. So, 5 and a half bucks becomes long a = 550; - as in, 550 dollarcents.

Now your entire process is ALL the same nature: "remove" as many whole units of a given denomination, print that, and move on to the next smaller denomination. And you never have to deal with rounding errors.

Scanner input = new Scanner(System.in);
double o = input.nextDouble();
long cents =(long) (100.0 * o);

You are also showing 0.001 units, so perhaps your atomary amount is not '100 per unit', but e.g. a 1000. The same principle applies. (If your currency has 120 doohickeys per sixpence, and amounts are customarily shown as (fractional) sixpences, then take the sixpences, multiply by 120, and do all your math on doohickeys).

Now, you just want to generalize the first part of your code, which is a nice programming exercise that you should do on your own. Here's a tip:

int[] denominations = [20000, 10000, 2000]; // this currency has bills of 200 dollars, 100 dollars, and 20 dollars).

for (int billSize : denominations) {
  int billCount = cents / billCount;
  cents -= billCount * billSize;
  // now 'billCount' contains how many of that bill you want to pay out.
}
  • Related