Using float to two decimal places

yes its me once again....
a human planet earth
ok......project coimng to a finish..... thank you to all who have responded to previous messages posted.... hopefully this may be the last... for a while anyway.....
var1 successfully parsed text to float
var2 successfully parsed text to float
float var3= var1 * var2
display of var1 2 and 3 ok
but need to reduce/control the number of decimal places being displayed.
in 'C' its easy but havent been able to find anything as yet under Java....
any pointers appreciated

(sigh...) java.text.DecimalFormat

Similar Messages

  • Is it possible to convert a float to two decimal places

    Can anyone help me to convert a float to two decimal places
    float f=16;
    System.out.println(f);
    It will print 16.0
    But I want to get printed as 16.00

    "convert" no, as no conversion is needed, obviously. Format the output, yes. Read the API for String and its printf method.

  • Rounding off a float to two decimal places

    I want to make a function where I shall pass a float and and integer.
    The float shall have to be rounded off to a value in the integer.
    Can anyone please suggest how to round off a float.
    E.g.: if the float is 12.56890 and I want to round it off to 2 decimal places, then it should be 12.57.
    Regards
    Hawker

    I didn't mention any datatypes like float, double.True, but that is what the question is about, so you weren't answering the question. For a change.
    As I mentioned, that was just a mathematical steps to round of the floating point value. (Not in any programming languages point of view).False. You didn't mention that at all.
    This is the code for that in java.So here you are mentioning datatypes and floats for the same piece of mathematics that you have already been told, with reasons, doesn't work in floating point.
    which seems to be working fine
    Seems to. What evidence do you have that the float actually got rounded? As opposed to got displayed as rounded? Which is not what the OP asked for.
    And of course all that code seems to do is round 0.01 to two decimal places, which again is not what the OP asked for.
    For any remaining fans of this 'technique', please explain the behaviour of the following code:
         public static void     main(String[] args)
              int     count = 0, errors = 0;
              for (double x = 0.0; x < 1; x += 0.0001)
                   count++;
                   double     d = x;
                   int     scale = 2;
                   double     factor = Math.pow(10, scale);
                   d = Math.round(d*factor)/factor;
                   if ((d % 0.01) != 0.0) // if 'd' really has been rounded this should be zero
                        System.out.println(d % 0.01);
                        errors++;
              System.out.println(count+" trials "+errors+" errors");
         }

  • Storeing values in floate data type upto two decimal places

    Hi
    I want a float type field in my table but want to store value up to two decimal places.
    so what syntax i need to follow plz suggest.
    thanks

    it is not possible to store with Number datatype. if you are using the data to sho report, then change its format (like 45 to 45.00). if you want to store 45.00 in database, then you have to modify the datatype for that column. for this follow the following path
    1. Add a column of datatype Varchar2.
    2. Copy all the value of your number field to it.
    3. Drop the old column.
    4. However make sure that if you have used that column somewhare in arithmetic calculation, the modify it and use to_number function to do arithmetic opration.

  • How to take float precision to two decimal places?

    Hi, everybody:
    I'm a straight noob...first week in Java programming. Before you bash me for not checking FAQs and such, believe me, I have.
    I have tried BigDecimal movePointLeft, but I don't know how to construct it using proper syntax. I also tried currency and toString but I keep messing up my syntax somehow. Four hours into this and nothing is working.
    Here is the problem: I have a float. I want to format its output to two decimal places, trying to achieve in Java what C would produce as:
    printf("The amount you owe is $%.2f", fltAnyName);
    Any help would be appreciated.
    Thanks,
    Rob

    Sorry, guys. I have tried both recommendations and I can't get either to work. I'm getting a compile error pointing to the delineating period between the "out" and "printf" when I try the C-like code. I have 1.5, too. I know your advice is sound so I must be doing something wrong. Can you insert the code DecimalFormat code that will correct this problem where it needs to go? The packages below have been included becasue of all the different solutions I have attempted.
    import java.io.*;
    import java.math.*;
    import java.text.*;
    import java.lang.*;
    public class CoinCalculator2
         public static void main(String[] args) throws IOException
              String strQuartes, strDimes, strNickels, strPennies;
              int intQuartes, intDimes, intNickels, intPennies;
              float fltValue;
              BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
              System.out.println("THE COIN CALCULATOR");
              System.out.println();
              System.out.print("Enter the number of quarters you have: ");
                   strQuartes = dataIn.readLine();
                   intQuartes = Integer.parseInt(strQuartes) * 25;
              System.out.print("Enter the number of dimes you have: ");
                   strDimes = dataIn.readLine();
                   intDimes = Integer.parseInt(strDimes) * 10;
              System.out.print("Enter the number of nickles you have: ");
                   strNickels = dataIn.readLine();
                   intNickels = Integer.parseInt(strNickels) * 5;
              System.out.print("Enter the number of pennies you have: ");
                   strPennies = dataIn.readLine();
                   intPennies = Integer.parseInt(strPennies);
              fltValue = (intQuartes + intDimes + intNickels + intPennies) / 100;
              System.out.println();
              System.out.println("The value of your coins is $" + fltValue);
              System.out.println();
    }

  • Help!printing float values with two decimal places

    hi there java pips! im a newbie to this technology so forgive me for this really stupid question....i would like to perform mathematical operations on two float values....the problem is i want to print them in a standard format and that is i want them to be displayed with two decimal places (e.g 190.00, 12,72, 1,000.01) how can i do this?

    Try java.text.DecimalFormat
    NumberFormat nf = new DecimalFormat("0.00");
    System.out.println(nf.format(x));

  • How can I limit a double value to two decimal place?

    How can I limit a double value to two decimal place?
    Java keeps on adding zero's to a simple double subtraction:
    1497 - 179.64 = 1317.3600000000001The answer must have been simply: 1317.36

    If the trouble is with output ...
    If the trouble is with value accuracy ...The trouble is with OPs understanding of and/or expectations of IEEE 754 floating point numbers. o_O
    [And it's probably a view (output) issue.]
    how can i actually use numberformat to cut those
    unwanted decimal places?Read the API - Puce already provided the link.

  • Two decimal places

    Hello, i want to have my results in my shop apllication in two decimal places. I tried DecimalFormat f = new DecimalFormat("#.##"); with import java.text.DecimalFormat; but it doesnt change the decimal places.
    And when i try this class example which i found on http://www.roseindia.net/java/beginners/RoundTwoDecimalPlaces.shtml it says that inner classes cannot have static declarations on public static void main(String[] args) {          and     public static float Round(float Rval, int Rpl) {
    Im using netbeans 5,5, does someone know what the problem is?
    public class RoundTwoDecimalPlaces{
    public static void main(String[] args) {
    float num = 2.954165f;
    float round = Round(num,2);
    System.out.println("Rounded data: " + round);
    public static float Round(float Rval, int Rpl) {
    float p = (float)Math.pow(10,Rpl);
    Rval = Rval * p;
    float tmp = Math.round(Rval);
    return (float)tmp/p;
    Edited by: javaboy on Nov 11, 2007 12:46 PM

    package view;
    import java.awt.Color;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.ArrayList;
    import javax.swing.JButton;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import controller.index;
    import model.product;
    import java.text.*;
    public class View_basket extends JPanel implements ActionListener{
         private static final long serialVersionUID = 1L;
         private index index;
         private JButton btnGoToPay;
         private JLabel[] lblProducts, lblPrices;
         private int countProducts;
         private int offset=40;          // # of pixels from the top
         private int increment=20;     // the next product will appear this amount of pixels below it's predecessor
         private JPanel container;
         public View_basket(index index){
              this.container = new JPanel(null);
              this.container.setName("Shopping basket");
              this.container.setBackground(new Color(151, 186, 225));          
              this.container.setSize(190,200);
              this.container.setLocation(550,20);
              this.index=index;
         public JPanel updateGui(){
              System.out.println("class view.View_basket.updateGui()");
              ArrayList products = index.basket.getProducts();
              countProducts = products.size();
              // if product basket is to big, resize the basket frame
              if(countProducts > 6){
                   this.container.setSize(190,(300+((countProducts-6)*10)));     
              lblProducts = new JLabel[countProducts];
              lblPrices = new JLabel[countProducts];
              JLabel lblTitle = new JLabel();
              lblTitle.setText("Winkelmand");
              lblTitle.setBounds(5, 5, 150, 20);
              lblTitle.setFont(index.FONT_12_BOLD);
              this.container.add(lblTitle);
              int teller=offset;
              double total=0;
              // walk trough all values from the arrayList
              for(int i=0; i < countProducts; i++){
                   product product = (product)products.get(i);
                   lblProducts[i] = new JLabel(product.toString());
                   lblProducts.setBounds(5, teller, 130, 20);
                   lblProducts[i].setFont(index.FONT_10_PLAIN);
                   this.container.add(lblProducts[i]);
                   lblPrices[i] = new JLabel("? "+ product.getPrice());
                   lblPrices[i].setBounds(140, teller, 150, 20);
                   lblPrices[i].setFont(index.FONT_10_PLAIN);
                   this.container.add(lblPrices[i]);
                   teller +=increment;
                   total += product.getPrice();
              // create total labels
              JLabel lblTotal = new JLabel("Totaal: ");
              lblTotal.setBounds(5, teller, 50, 20);
              lblTotal.setFont(index.FONT_10_BOLD);
              this.container.add(lblTotal);
              // create total labelseeuro
              JLabel lblTotalPriceeuro = new JLabel("? "+ total);
              lblTotalPriceeuro.setBounds(140, teller, 50, 20);
              lblTotalPriceeuro.setFont(index.FONT_10_BOLD);
              this.container.add(lblTotalPriceeuro);
    // create total labelsdollar
              JLabel lblTotalPricedollar = new JLabel("$ "+ total*1.45);
              lblTotalPricedollar.setBounds(140, teller, 50, 40);
              lblTotalPricedollar.setFont(index.FONT_10_BOLD);
              this.container.add(lblTotalPricedollar);
              int btnOffset = this.container.getHeight()-25;
              btnGoToPay = new JButton("Betalen...");
              btnGoToPay.setBounds(5, btnOffset, 180, 20);
              btnGoToPay.setFont(index.FONT_12_BOLD);
              btnGoToPay.addActionListener( this );     
              if(countProducts == 0)
                   btnGoToPay.setEnabled(false);
              this.container.add(btnGoToPay);
              return container;
         public void actionPerformed(ActionEvent event) {
              // if the pay button is pressed
              if(event.getSource().equals(btnGoToPay)){
                   ((View_pay) index.panelPay).updateGui();
                   index.showPanel("View_pay");

  • Trunc percentage two decimal places

    I have a report that has the option of dispalying pdf or excel. I have the pdf version working fine but i am having a little trouble on the excel version. I have a grand total and department total. Basically I need help formating two decimal places. Both dept_total and grand_total are numbers that both display. I have another column that displays what percent of the total is from each dept.It currently returns a number such as 10.23383547957459973 which i need to display as 10.23 How can i incorporate the trunc function into this code?
    ...to_char((dept_total/grand_total) *100)
    The query looks similar to this.
    select
    to_char(a._grand_total)
    ,to_char(a.dept_total)
    ,to_char((dept_total/grand_total) *100)
    from(
    select sum(xxxx) grand_total,
    sum(xxxx) dept_total
    from table)a

    Hi,
    Since you're calling TO_CHAR anyway, you can add a 2nd argument, specifying two decimal places:
    select
         a._grand_total
    ,     a.dept_total
    ,     to_char ((dept_total/grand_total) *100,       '999999999.99')     AS pct
    from     (
             select  sum (xxxx) grand_total,
                      sum (xxxx) dept_total     -- Why have two copies of the same value?
             from    table
         )a If you're not using a 2nd argument, why use TO_CHAR at all in this query?
    This will round the value, not truncate it. That is, if the value is 10.23<b>5</b>83547957459973, it would display '10.2<b>4</b>', not '10.2<b>3</b>'. If you always want to round toward 0 (rather than to the nearest multiple of .01), then use TRUNC.

  • Uploading Number with Two Decimal Places

    I am trying to import data from my excel file to table in the database using SQL Developer.
    It is working fine except that in my excel file I have data with two decimal places, but during import it shows only one decimal place. Is there any way I can import data with two decimal places?

    This works well with SQL Dev 1.5.X.
    Guess it would have been a bug in prior versions.

  • Reduce the field value from three decimal places to two decimal places

    hi experts,
    plz help me with this
    actually i have to display the  value of the field MENGE in the list with two decimal places.
    but when i go and see the field menge in the table mseg it is having three decimal places.
    so could u plz suggest me how to solve this.
    regards
    siri

    Please try using the DEIMALS extension of the WRITE statement.
    data: menge type menge_D value '1234'.
    write:/ menge decimals 2.
    Regards,
    RIch HEilman

  • Any function module to round of a value to two decimal places?

    Hi gurus,
    Any function module to round of a value to two decimal places?

    Hi,
    you can use FM 'ROUND'.
    Thanks.

  • Price per unit is getting round off to two decimal places.

    Hi Gurus,
    In the Billing output, the Price per unit is getting round off to two decimal places.
    How can we avoid this or allow 4 decimal places for such cases.
    In the same pricing procedure, we have two pricing condition types PR02 and ZR01.
    When PR02 is being used,it doesnt rounds off Price per unit.e.g .578888 = .578888
    But when ZR01 is being used the system rounds off price per unit.e.g.578888 = .58
    Please suggest where is the setting for it.
    Thanks
    Montee

    Hi,
    Which currency you are using? If its INR, then it will do only for 2 decimal places.
    The solution for ur problem can be ractified by changing the scales :
    Also check OSS 80183
    Thanks,
    Raja

  • How do I round calculations to two decimal places (form-wide)

    Hi,
    I've created an order form where customers input the quantity of product they want to order and the form outputs the total due (including shipping and sales tax). Depending on user input, the resulting total sometimes contains more than two decimal places. How do I round calculations to two decimal places? Is there a way to do this form-wide?
    I'm new at using Acrobat for calculations. Any help would be greatly appreciated! Thanks...

    The above will affect the displayed value not the actual value of the field nor its value when accessed in another computation. This behavior may cause an error of 1 cent or more in the grand total or sales tax computation. If you want the the value and displayed value to be the same value you can use the following Validation script:
    event.value = util.scand("%,1 0.2f", event.value);

  • Need to restrict numeric entry to two decimal places

    Hello,
    I have a cfform with a cfinput of type=text. I need to restrict what can be entered into the cfinput so that it allows only two decimal places. I have tried the mask, but it won't work because "99999.99" will not allow 12.34 and "99.99" will not allow 12345.67.
    I have read about validate and pattern but I've never used them. I don't know JavaScript, but I can copy and paste pretty well.
    Any help you can provide is greatly appreciated.
    Thank you,
    Jim

    *795589,*
    What particular percent complete are you having trouble with?
    *(Found in the P6 knowledgebase) This issue is an existing Enhancement Request. It will be considered for a future release*
    Other than that, maybe try creating a global change to manually calculate your percents.
    hope this helps,
    +*** Can the number of decimal places for Percent Complete be changed? [ID 895884.1] Modified 30-OCT-2009+

Maybe you are looking for