[英]I need to be able to capture decimals with only 2 digits after the decimal point java, tried this
我需要能夠捕獲小數點后僅2位的小數
點java,嘗試了這個
這是我的代碼,我需要知道能夠讀取2個小數位
package project.pkg1;
import javax.swing.JOptionPane;
/**
* @author Maria Andrea Quintana 4891405 *
* @author Salvador Frias
*/
public class Project1 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
String s1 = JOptionPane.showInputDialog("Enter the purchase amount");
if (s1 == null) {
JOptionPane.showMessageDialog(null, "You must enter a valid integer");
System.exit(0);
}
if (s1.isEmpty()) {
JOptionPane.showMessageDialog(null, "You must enter a valid integer");
System.exit(0);
}
for (int i = 0; i < s1.length(); i = i + 1) {
if (!Character.isDigit(s1.charAt(i))) {
JOptionPane.showMessageDialog(null, "You must enter an integer value");
System.exit(0);
}
}
您可以使用DecimalFormat格式化雙精度型:
double input = 455.1689;
DecimalFormat df = new DecimalFormat("#.00");
System.out.println(df.format(input));
看這個問題處理同樣的問題。
編輯2:
刪除for循環並將此代碼放置在原處
double input = Double.parseDouble(s1);
DecimalFormat df = new DecimalFormat("#.00");
s1 = df.format(input);
嘗試這樣:
s1
是您的文本或字符串值:
String s=String.format("output: %.2f", s1);
System.out.println(s);
由於您使用的是可以使用不同貨幣單位的貨幣 。 我建議您看一下Moneta API
<!-- Moneta -->
<dependency>
<groupId>org.javamoney</groupId>
<artifactId>moneta</artifactId>
<version>0.9</version>
</dependency>
MonetaryAmount monetaryAmount = MonetaryAmounts.getDefaultAmountFactory()
.setNumber(3.45)
.setCurrency("USD")
.create();
System.out.println("EXACT MONETARY VALUE: "+monetaryAmount.getNumber().doubleValueExact()); // 3.45
System.out.println("VALUE AFTER DECIMAL: "+monetaryAmount.getNumber().getAmountFractionNumerator()); // 45
如果允許使用正則表達式 ,我想這就是您要尋找的
String s1 = JOptionPane.showInputDialog("Enter the purchase amount");
double d = 0.0;
int counter = 0;
Matcher match = Pattern.compile("[aA-zZ]+").matcher(s1);
while (match.find()) {
counter++;
}
if((!s1.isEmpty()) && (s1.trim().length()!=0) && (counter == 0)){
if(s1.contains(".")){
String[] strings = s1.split("\\.");
if (strings[1].length() > 2) {
System.out.println("Maximum 2 places allowed after decimal.");
System.exit(0);
}
}
d = Double.parseDouble(s1);
}else if(counter > 0){
System.out.println("Only numeric values are allowed");
System.exit(0);
}
System.out.println("Expected input retrieved: " + d);
解決了,
String s1 = JOptionPane.showInputDialog("Enter the purchase amount");
if (s1 == null) {
JOptionPane.showMessageDialog(null, "You must enter a valid integer");
System.exit(0);
}
if (s1.isEmpty()) {
JOptionPane.showMessageDialog(null, "You must enter a valid integer");
System.exit(0);
}
if (s1.equals("0")) {
JOptionPane.showMessageDialog(null, "You must enter a valid integer");
System.exit(0);
}
if (s1.matches("[a-zA-Z]+")) {
JOptionPane.showMessageDialog(null, "You must enter a valid integer");
System.exit(0);
}
double input = Double.parseDouble(s1);
DecimalFormat df = new DecimalFormat("#.00");
s1 = df.format(input);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.