corsasport.co.uk
 

Corsa Sport » Message Board » Off Day » Geek Day » Java


New Topic

New Poll
  Subscribe | Add to Favourites

You are not logged in and may not post or reply to messages. Please log in or create a new account or mail us about fixing an existing one - register@corsasport.co.uk

There are also many more features available when you are logged in such as private messages, buddy list, location services, post search and more.


Author Java
TheMason66
Member

Registered: 28th Nov 07
Location: Peterborough
User status: Offline
3rd Oct 08 at 18:36   View User's Profile U2U Member Reply With Quote

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
3rd Oct 08 at 21:41   View User's Profile U2U Member Reply With Quote

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
4th Oct 08 at 10:50   View User's Profile U2U Member Reply With Quote

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
4th Oct 08 at 18:22   View User's Profile U2U Member Reply With Quote

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
5th Oct 08 at 15:34   View User's Profile U2U Member Reply With Quote

The code compiles fine, but the actual answers are wrong. I will tweak it a bit and see what happens. Cheers 4 ur help!

 
New Topic

New Poll

  Related Threads Author Forum Replies Views Last Post
What Java Compiler Is Standard? Mikorsa16v Geek Day 2 1310
13th Mar 03 at 16:02
by Mikorsa16v
 
help computer boffs groom Geek Day 33 2963
13th Nov 03 at 21:00
by Tim
 
How do you use the java multi upload thing jr General Chat 23 1350
25th Jun 06 at 14:35
by Gaz
 
Trouble with Azureus andy1868 Geek Day 8 937
30th Aug 07 at 17:29
by Richie
 

Corsa Sport » Message Board » Off Day » Geek Day » Java 29 database queries in 0.0093369 seconds