Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java代码中引入浮点值?_Java - Fatal编程技术网

如何在Java代码中引入浮点值?

如何在Java代码中引入浮点值?,java,Java,您可能正在寻找的答案是Float.parseFloat()或Double.parseDouble(),具体取决于所需的精度 如果您碰巧接受不同格式的货币/数值(取决于区域设置),请尝试NumberFormat方法(传递有效的区域设置对象)。浮动/双精度是个坏主意。美元是一种固定的十进制计算方法 import java.io.*; public class CheckbookBalancingCalculator { public static void main( String[] a

您可能正在寻找的答案是Float.parseFloat()或Double.parseDouble(),具体取决于所需的精度


如果您碰巧接受不同格式的货币/数值(取决于区域设置),请尝试NumberFormat方法(传递有效的区域设置对象)。

浮动/双精度是个坏主意。美元是一种固定的十进制计算方法

import java.io.*;

public class CheckbookBalancingCalculator {
    public static void main( String[] args ) throws IOException {
        String statementbalance, depositstotal, checkstotal, feestotal;
        int statement, deposits, checks, fees, index;
        BufferedReader dataIn = new BufferedReader( new InputStreamReader( System.in ));

        System.out.println( "\tBalancing Your Checkbook:" );
        System.out.println();
        System.out.print( "\t\tWhat is the balance from your last statement?" );
        statementbalance = dataIn.readLine();
        statement = Integer.parseInt( statementbalance );
        System.out.print( "\t\tWhat is the total amount of all deposits?" );
        depositstotal = dataIn.readLine();
        deposits = Integer.parseInt( depositstotal );
        System.out.print( "\t\tWhat is the total amount of all checks?" );
        checkstotal = dataIn.readLine();
        checks = Integer.parseInt( checkstotal );
        System.out.print( "\t\tWhat is the total amount of all transaction fees?" );
        feestotal = dataIn.readLine();
        fees = Integer.parseInt( feestotal );

        index = statement + deposits - checks - fees;

        System.out.println();
        System.out.println( "\tYour new balance is " + Math.round( index ) + "." );
        System.out.println();
    }
}
这太离谱了。然后用BigDecimal做数学运算

String statementBalanceInput, depositsTotalInput, checksTotalInput, feesTotalInput;
int statement, deposits, checks, fees, index;

BigDecimal statementBalance = new BigDecimal( statementBalanceInput);
BigDecimal depositsTotal = new BigDecimal( depositsTotalInput);
BigDecimal checksTotal = new BigDecimal( checksTotalInput);
BigDecimal feesTotal = new BigDecimal( feesTotalInput);
导入java.io.*; 公共类支票簿平衡计算器{ 公共静态void main(字符串[]args)引发IOException{ 字符串语句余额、存款总额、支票总额、金额总额; 浮动报表、存款、支票、费用、指数; BufferedReader dataIn=新的BufferedReader(新的InputStreamReader(System.in)); System.out.println(“\t平衡支票簿:”); System.out.println(); System.out.print(“\t\t上次对账单的余额是多少?”); statementbalance=dataIn.readLine(); 语句=Float.parseFloat(语句余额); System.out.print(“\t\t所有存款的总额是多少?”); Depositotal=dataIn.readLine(); 存款=浮动。分析浮动(存款总额); System.out.print(“\t\t所有支票的总金额是多少?”); checkstotal=dataIn.readLine(); checks=Float.parseFloat(checkstotal); System.out.print(“\t\t所有交易费用的总额是多少?”); feestotal=dataIn.readLine(); 费用=浮动。分析浮动(feestotal); 索引=对账单+存款-支票-费用; System.out.println(); System.out.println(“\t您的新余额为“+Math.round(index)+”); System.out.println(); } }
这里真正的问题是什么?@Noel-看看他得到的答案。我认为问题是他没有得到足够的答案来接受最好的答案@nolimitsplayer-措辞更清晰的问题将鼓励更多数量和质量的回答?你的意思当然是浮动。-1没有课
单人
谢谢,我当然是指浮动。“我猜是太多的C#了。”埃里克-1为一个非常糟糕的问题给出了一个有趣的答案。贝塞德斯,我已经做了你在问题标题中找到的。上次我检查时,这不是一个幽默的网站。我对有帮助的回答投赞成票,对没有帮助的回答投反对票。@Erick叹息你把它看得太严肃了,这就是我想给你看的。这是一个非常糟糕的问题,我已经正确地回答了,但没有帮助。当然没有正确答案。我可以向你保证,这个答案并没有说明问题的意图。@InsertNickHere我了解你的幽默感!:) import java.io.*; public class CheckbookBalancingCalculator { public static void main( String[] args ) throws IOException { String statementbalance, depositstotal, checkstotal, feestotal; float statement, deposits, checks, fees, index; BufferedReader dataIn = new BufferedReader( new InputStreamReader( System.in )); System.out.println( "\tBalancing Your Checkbook:" ); System.out.println(); System.out.print( "\t\tWhat is the balance from your last statement?" ); statementbalance = dataIn.readLine(); statement = Float.parseFloat( statementbalance ); System.out.print( "\t\tWhat is the total amount of all deposits?" ); depositstotal = dataIn.readLine(); deposits = Float.parseFloat( depositstotal ); System.out.print( "\t\tWhat is the total amount of all checks?" ); checkstotal = dataIn.readLine(); checks = Float.parseFloat( checkstotal ); System.out.print( "\t\tWhat is the total amount of all transaction fees?" ); feestotal = dataIn.readLine(); fees = Float.parseFloat( feestotal ); index = statement + deposits - checks - fees; System.out.println(); System.out.println( "\tYour new balance is " + Math.round( index ) + "." ); System.out.println(); } }