我需要能够捕获小数点后只有2位的小数,java,尝试过这个

我需要能够捕获小数点后只有2位的小数,java,尝试过这个,java,floating-point,int,double,decimal,Java,Floating Point,Int,Double,Decimal,我需要能够捕获小数点后只有2位的小数点 点java,试试这个 这是我的代码,我需要知道是否能够读取小数点后2位 package project.pkg1; import javax.swing.JOptionPane; /** * @author Maria Andrea Quintana 4891405 * * @author Salvador Frias */ public class Project1 { /** * @param args the comma

我需要能够捕获小数点后只有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);
            }
        }
package project.pkg1;
导入javax.swing.JOptionPane;
/**
*@作者Maria Andrea Quintana 4891405*
*@作者萨尔瓦多·弗里亚斯
*/
公共类项目1{
/**
*@param指定命令行参数
*/
公共静态void main(字符串[]args){
//此处的TODO代码应用程序逻辑
字符串s1=JOptionPane.showInputDialog(“输入购买金额”);
如果(s1==null){
showMessageDialog(null,“必须输入有效整数”);
系统出口(0);
}
if(s1.isEmpty()){
showMessageDialog(null,“必须输入有效整数”);
系统出口(0);
}
对于(int i=0;i
您可以使用十进制格式格式化您的双精度:

    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);

因为你工作的钱可能不同,我建议你看看

要添加的Maven依赖项: 编辑: 如果你被允许使用,我想这就是你想要的

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);

您需要小数点后的两位数字,例如,如果输入为455.1689,则应为455.16,对吗?是的,基本上我需要一个小数点和两个位置当处理货币值时,请查看此处:@mariaandreaerramouspe不要替换这样的问题。只需单击答案旁边的复选标记接受您喜欢的答案。问题已还原。我是新手,不知道这会是个问题,抱歉我不明白,我想修改我拥有的,所以我应该为那个写另一个if吗?你能告诉我们s1是什么吗?一个文本字段?一个字符串?一个字符串对不起…s1是我应该输入的销售额,或者我为其他计算给出的名称/?你能告诉我们吗编辑您的问题并发布您的所有代码?这将帮助我们了解您试图执行的操作。我只是不知道如何在s1方面执行此操作。我想对其进行修改,例如(int i=0;iString 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);