Solved

Formating float output in a gui textfield

Posted on 2007-04-05
6
223 Views
Last Modified: 2010-03-31
Hi Experts,
Just a quick one. I have a banking system that i am programming for an assignment, output is via a GUI, i have everything working ut am not happy with the output of float values, i am outputing in 4 dp when i only want 2 dp. Can you help, 200 points fo a quick answear the relevant code is below

private void displayAccountDetails()
        {
            currentSelectedCustomer = branch.getCustomerAt(customerComboBox.getSelectedIndex());
            balanceTextField.setText("");
          limitTextField.setText("");
            interestTextField.setText("");
          //depending on the account type....
          if(accountTypeComboBox.getSelectedIndex() == 0)
            {
               
              CreditCardAccount acc = currentSelectedCustomer.getCreditCardAccount();
            accountNumberTextField.setText(acc.getAccNumber());
            balanceTextField.setText(""+acc.getBalance());
            limitTextField.setText("Limit: " + acc.getLimit());
                interestTextField.setText("Rate: " + acc.getCreditCardRate());
          }
          else
              if(accountTypeComboBox.getSelectedIndex() == 1)
                {
                CurrentAccount acc = currentSelectedCustomer.getCurrentAccount();
                accountNumberTextField.setText(acc.getAccNumber());
                balanceTextField.setText(""+acc.getBalance());
                limitTextField.setText("Limit: " + acc.getLimit());
            }
          else
              if(accountTypeComboBox.getSelectedIndex() == 2)
                {
                SavingsAccount acc = currentSelectedCustomer.getSavingsAccount();
                accountNumberTextField.setText(acc.getAccNumber());
                balanceTextField.setText(""+acc.getBalance());
                interestTextField.setText("Rate: " + acc.getSavingsRate());
            }
      }

and the source for the balance

 //get account details
    public float getBalance() //selector method
    {
        return accBalance;
    }

Thanks in advance
0
Comment
Question by:manuman1968
  • 3
  • 2
6 Comments
 
LVL 5

Expert Comment

by:tbboyett
ID: 18861341
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 18861374
>> Formating float output

Define the field as a double, not a float.

http://www.exampledepot.com/egs/java.text/FormatNum.html
0
 

Author Comment

by:manuman1968
ID: 18861633
Sorry but thats completely lost me as a newcomer, any chance of showing me an example of code.
Thanks
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 5

Accepted Solution

by:
tbboyett earned 500 total points
ID: 18861644
It would be something like this.  You can set the DecimalFormat to what ever you want the number to look like.

DecimalFormat df = new DecimalFormat("###.00");
double mynumber = 245.4444;
TextField.setText(df.format(mynumber));
0
 
LVL 5

Expert Comment

by:tbboyett
ID: 18861647
don't forget to do the import, i believe it's java.text.

import java.text.*;
0
 

Author Comment

by:manuman1968
ID: 18861701
Thanks that works
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
recursion example 16 124
egit plugin on eclipse 8 62
Android development question 2 34
Detect Closed Loops (circles, figure-8s, etc) in PNG Images 6 31
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now