The Code-Bin
Links
Home
Add your code!
All Listings
About
Latest Entry
Featured Scripts
Author's Website
Latest Entries
FFMPEG Thumbnail Scr...
PHP, 0.8KB
Jul. 29, 10:24pm
John
Z80 Assembler, 190 bytes
Feb. 17, 3:36am
John
Z80 Assembler, 176 bytes
Sep. 13, 2:19am
John
Z80 Assembler, 77 bytes
Sep. 13, 2:18am
John
Z80 Assembler, 209 bytes
Sep. 13, 2:17am
untitled Java 5 Code
Posted by: Stewart | March 12, 2011 @ 7:42pm
Java 5 Code
[
Download
]
//This uses int casting. Casting is a way of getting the value of an expression and allowing it to be stored in another variable type without directly effecting the math that it does. public class currencyConverter { public static void main( String[] args ){ double totalAmount = Double.valueOf( args[ 0 ] ); //takes any arguments from the commandline as uses them as input. Since commandline arguments are strings, you have to convert to int to preserve the cents int totalHundredDollars = (int)totalAmount / 100; //divides the total amount of money you have by 100 to determine the amount of 100 dollar bills you can use totalAmount = totalAmount % 100; //replaces the current amount with the amount remaining after the calculation from the previous line int totalFiftyDollars = (int)totalAmount / 50; totalAmount = totalAmount % 50; int totalTwentyDollars = (int)totalAmount / 20; totalAmount = totalAmount % 20; int totalTenDollars = (int)totalAmount / 10; totalAmount = totalAmount % 10; int totalFiveDollars = (int)totalAmount / 5; totalAmount = totalAmount % 5; int totalTwoDollars = (int)totalAmount / 2; totalAmount = totalAmount % 2; int totalDollars = (int)totalAmount / 1; totalAmount = totalAmount % 1; int totalFiftyCents = (int)( totalAmount / .5 ); totalAmount = totalAmount % .5; int totalTwentyCents = (int)( totalAmount / .2 ); totalAmount = totalAmount % .2; int totalTenCents = (int)( totalAmount / .1 ); totalAmount = Math.round( ( totalAmount % .1 ) * 100 ); //determines how many cents are left over after performing all previous calculations, then multiplies by 100 since the round method only rounds for one decimal place int totalFiveCents = (int)( totalAmount / 5 ); System.out.println( "100 dollars: " + totalHundredDollars ); System.out.println( "50 dollars: " + totalFiftyDollars ); System.out.println( "20 dollars: " + totalTwentyDollars ); System.out.println( "10 dollars: " + totalTenDollars ); System.out.println( "5 dollars: " + totalFiveDollars ); System.out.println( "2 dollars: " + totalTwoDollars ); System.out.println( "1 dollars: " + totalDollars ); System.out.println( "50 cents: " + totalFiftyCents ); System.out.println( "20 cents: " + totalTwentyCents ); System.out.println( "10 cents: " + totalTenCents ); System.out.println( "5 cents: " + totalFiveCents ); } }
Syntax Highlighting
[
Open in new window
]
Author Comments
none
Rating
3.51 / 8
70 Votes
http://codebin.yi.org/1165
page generated in 0.01 seconds