我需要能够捕获小数点后只有 2 位数字的小数 java,试过这个
I need to be able to capture decimals with only 2 digits after the decimal point java, tried this
我需要能够捕获小数点后只有 2 位的小数
点java,试过这个
这是我的代码,我需要知道能够读取小数点后两位
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));
看看this question处理同样的问题。
编辑 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);
因为你工作的钱可能不同currency unit's. I would suggest you to take a look at Moneta API
要添加的 Maven 依赖项:
<!-- 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
编辑:
如果你被允许使用regular expressions,我想这就是你要找的
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);
我需要能够捕获小数点后只有 2 位的小数
点java,试过这个
这是我的代码,我需要知道能够读取小数点后两位
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));
看看this question处理同样的问题。
编辑 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);
因为你工作的钱可能不同currency unit's. I would suggest you to take a look at Moneta API
要添加的 Maven 依赖项:
<!-- 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
编辑:
如果你被允许使用regular expressions,我想这就是你要找的
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);