• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 232
  • Last Modified:

Formating float output in a gui textfield

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
manuman1968
Asked:
manuman1968
  • 3
  • 2
1 Solution
 
Mayank SAssociate Director - Product EngineeringCommented:
>> Formating float output

Define the field as a double, not a float.

http://www.exampledepot.com/egs/java.text/FormatNum.html
0
 
manuman1968Author Commented:
Sorry but thats completely lost me as a newcomer, any chance of showing me an example of code.
Thanks
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
tbboyettCommented:
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
 
tbboyettCommented:
don't forget to do the import, i believe it's java.text.

import java.text.*;
0
 
manuman1968Author Commented:
Thanks that works
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now