Solved

Formating float output in a gui textfield

Posted on 2007-04-05
6
220 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:mayankeagle
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
triangle challenge 4 77
solarwind tftp server 2 31
@SBGen Method 3 25
Securing Jmx Console and web console 2 48
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

706 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

15 Experts available now in Live!

Get 1:1 Help Now