开发者

How do I introduce a float value in this Java code? [closed]

开发者 https://www.devze.com 2023-01-17 08:46 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 12 years ago.
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 to开发者_运维问答tal 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();
    }
}


Floats/Doubles are a bad idea. US Currency is a fixed decimal calculation.

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

This is off the top of my head. Then do the math with BigDecimal.


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


The answer you are probably looking for is Float.parseFloat() or Double.parseDouble(), depending on required precision.

If you happen to accept currency / numeric values in different formats, depending on locale, try NumberFormat methods instead (passing valid Locale object).

0

精彩评论

暂无评论...
验证码 换一张
取 消