TheMason66
Member
Registered: 28th Nov 07
Location: Peterborough
User status: Offline
|
Ive just started programming in java, and im trying to turn decimals into integers so i can show money in individual coin values. Ive got the calculation done but the conversion is starting to piss me off. The Math.round feature is one that should work, but I have no idea how to implement it all. Help me!!!!!!!!!!
|
Mike2k111
Member
Registered: 7th Oct 03
Location: N.Wales
User status: Offline
|
Int anInt = Math.round(float afloat);
Should do it
Also,
Float someDecimal = 1.5678
Int anInt = someDecimal.intValue();
[Edited on 03-10-2008 by Mike2k111]
|
TheMason66
Member
Registered: 28th Nov 07
Location: Peterborough
User status: Offline
|
Ok, rather than me trying to explain it, here is the code. I just need a way of turning the double values into rounded integers.
public class MoneyAddition
{
public static JavaInput jInput=new JavaInput();
public static double value;
public static double num1, num2, num3, num4, num5, num6, num7, num8;
public static void main()
{
jInput.printString("Please enter an amount in pounds and pence");
value=jInput.readDouble();
num1=(value/100);
num2=((value%100)/50);
num3=(value%50)/20;
num4=(value%20)/10;
num5=(value%10)/5;
num6=(value%5)/2;
num7=(value%2)/1;
jInput.printString("£1 = "+num1+"\n"+"50p = "+num2+"\n"+"20p = "+num3+"\n"+"10p = "+num4+"\n"+"5p = "+num5+"\n"+"2p = "+num6+"\n"+"1p = "+num7);
}
}
And i have tried the code above and it doesnt seem to like it!!
|
Mike2k111
Member
Registered: 7th Oct 03
Location: N.Wales
User status: Offline
|
Try:
public class MoneyAddition
{
public static JavaInput jInput=new JavaInput();
public static double value;
public static double num1, num2, num3, num4, num5, num6, num7, num8;
public static void main()
{
jInput.printString("Please enter an amount in pounds and pence");
value=jInput.readDouble();
num1=(value/100);
num2=((value%100)/50);
num3=(value%50)/20;
num4=(value%20)/10;
num5=(value%10)/5;
num6=(value%5)/2;
num7=(value%2)/1;
jInput.printString("£1 = "+Math.round(num1)+"\n"+"50p = "+Math.round(num2)+"\n"+"20p = "+Math.round(num3)+"\n"+"10p = "+Math.round(num4)+"\n"+"5p = "+Math.round(num5)+"\n"+"2p = "+Math.round(num6)+"\n"+"1p = "+Math.round(num7));
}
}
Or is that what you have been trying?
Do you get any compile error messages or is it running ok but not displaying as an integer?
[Edited on 04-10-2008 by Mike2k111]
|
TheMason66
Member
Registered: 28th Nov 07
Location: Peterborough
User status: Offline
|
The code compiles fine, but the actual answers are wrong. I will tweak it a bit and see what happens. Cheers 4 ur help!
|