This question already has an answer here:
这个问题在这里已有答案:
- Convert String to double in Java 12 answers
在Java 12答案中将String转换为double
I just have a question about adding some currency ($) in JAVA, I used the NumberFormat.getCurrencyInstance(); to get my outputs in "$". My program is to input some money (String format) for example the program only accepts ($100.00, $50.00, $20.00 ... and so on) so I used this code:
我刚才有一个关于在JAVA中添加一些货币($)的问题,我使用了NumberFormat.getCurrencyInstance();得到我的输出“$”。我的程序是输入一些钱(字符串格式),例如程序只接受($ 100.00,$ 50.00,$ 20.00 ......等等)所以我使用了这段代码:
String payment = keyboard.next();
while (!(payment.equals("$100.00")) && (!payment.equals("$50.00")) && (!payment.equals("$20.00")) && (!payment.equals("$10.00")) && (!payment.equals("$5.00")) && (!payment.equals("$2.00")) && (!payment.equals("$1.00")) {
System.out.print("Invalid coin or note. Try again. ");
payment = keyboard.next(); }
How can I get the inputs (100.00, 50.00 ... ) as a Double in order to subtract them from the total price.. for example I want (100.00-12.00) (12.00 is the total price)
我怎样才能将输入(100.00,50.00 ......)作为Double来从总价中减去它们...例如我想要(100.00-12.00)(12.00是总价)
Any help would be appreciated Thanks
任何帮助将不胜感激谢谢
2 个解决方案
#1
0
If your input is a "$" sign, you can delete the first element of your string and then convert it to a double.
如果您的输入是“$”符号,则可以删除字符串的第一个元素,然后将其转换为double。
//method to convert String in Double
public Double getDoubleFromString(String payment)
{
payment = payment.substring(1);
double paymentDouble = Double.parseDouble(payment);
return paymentDouble;
}
String payment = keyboard.next();
double paymentDouble = getDoubleFromString(payment);
while (paymentDouble != 100.00 && paymentDouble != 50.00 && paymentDouble != 20.00
&& paymentDouble != 10.00 && paymentDouble != 5.00
&& paymentDouble != 2.00 && paymentDouble != 1.00)
{
System.out.print("Invalid coin or note. Try again.");
String payment = keyboard.next();
paymentDouble = getDoubleFromString(payment);
}
#2
1
public double convertPayment(String inputPayment) {
String payment = inputPayment.substring(1);
double paymentValue = Double.parseDouble(payment);
return paymentValue;
}
#1
0
If your input is a "$" sign, you can delete the first element of your string and then convert it to a double.
如果您的输入是“$”符号,则可以删除字符串的第一个元素,然后将其转换为double。
//method to convert String in Double
public Double getDoubleFromString(String payment)
{
payment = payment.substring(1);
double paymentDouble = Double.parseDouble(payment);
return paymentDouble;
}
String payment = keyboard.next();
double paymentDouble = getDoubleFromString(payment);
while (paymentDouble != 100.00 && paymentDouble != 50.00 && paymentDouble != 20.00
&& paymentDouble != 10.00 && paymentDouble != 5.00
&& paymentDouble != 2.00 && paymentDouble != 1.00)
{
System.out.print("Invalid coin or note. Try again.");
String payment = keyboard.next();
paymentDouble = getDoubleFromString(payment);
}
#2
1
public double convertPayment(String inputPayment) {
String payment = inputPayment.substring(1);
double paymentValue = Double.parseDouble(payment);
return paymentValue;
}