Setting what currency i want with getCurrencyInstance()
Hi all, like in my post title, how do i set an alternative currency to print out.
For example with the following program it is printing out in £ format. Im guessing this is to do with my Locale and the fact Im in the UK.
How would i print this out with the dollar sign then? without changing any settings.
Thanks
Code :
// ask user how many quarters, dimes and nickels they have
// print out how much money the user has, format the output to display currency symbol
import java.util.Scanner;
import java.text.NumberFormat;
public class usaCurrencyAdd {
public static void main(String[] args) {
final double quarterValue = 0.25;
final double dimeValue = 0.10;
final double nickelValue = 0.05;
int quartersQuantity, dimesQuantity, nickelsQuantity;
double quartersTotal, dimesTotal, nickelTotal, grandTotal;
Scanner scan = new Scanner(System.in);
System.out.println("How many quarters do you have?");
quartersQuantity = scan.nextInt();
System.out.println("How many dimes do you have?");
dimesQuantity = scan.nextInt();
System.out.println("How many nickels do you have?");
nickelsQuantity = scan.nextInt();
quartersTotal = quarterValue * quartersQuantity;
dimesTotal = dimeValue * dimesQuantity;
nickelTotal = nickelValue * nickelsQuantity;
grandTotal = quartersTotal + dimesTotal + nickelTotal;
NumberFormat fmt = NumberFormat.getCurrencyInstance();
System.out.println(fmt.format(grandTotal));
}
}
Re: Setting what currency i want with getCurrencyInstance()
Two options:
1. Create your own Currency object based upon a Locale of your choice.
2. Use DecimalFormat where you can specify the format: "$#.##" or whatever you like.