Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Formating float output in a gui textfield

Posted on 2007-04-05
6
224 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
even odd program using while loop 3 60
hibernate jars 4 45
ejb entity bean example issue 2 16
Eclipse Help Java EE 5,6,7 Documentation, why not Java EE 8 8 30
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

856 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