[英]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.