Results 1 to 5 of 5

Thread: How to convert Double data type value into Integer one in Java

  1. #1
    Join Date
    Aug 2008
    Posts
    90

    How to convert Double data type value into Integer one in Java

    Can someone tell me How do we convert Double value in Integer format, that is I have a little program whose output value comes into 5 precision value. I want to display that value and after displaying that value on the next line it should be displayed as rounded off value. i.e in the Integer format does this possible. Please help.

  2. #2
    Join Date
    Feb 2006
    Posts
    172

    Re: How to convert Double data type value into Integer one in Java

    Following is an simple example where you could have your answer, you just need to have the simple logic for what you want.

    Code:
    double f = 17.30;
    int decimal_Place = 5;
    BigDecimal cd = new BigDecimal( Double.toString(d) );
    bd = bd.setScale( decimal_Place, BigDecimal.ROUND_HALF_UP );
    System.out.println( bd.doubleValue() );

  3. #3
    Join Date
    Oct 2008
    Posts
    167

    Re: How to convert Double data type value into Integer one in Java

    Just simply copy paste the code on the notepad and save it as Demo.java and while running this code used javac demo this will show you expected result, If I not mistaking, I am also newbie to the JAVA programming.

    Code:
    public class Demo
    {
        public static void main(String[] args)
        {
            double val = 3.14159;
            String s;
    
            s = String.format("%.2f", val);
            System.out.println(s);
        }
    }

  4. #4
    Join Date
    Jan 2009
    Posts
    99

    Re: How to convert Double data type value into Integer one in Java

    DecimalFormat is a concrete subclass of NumberFormat that formats decimal numbers. if you really need exact representation of decimal fractions (usually the case only for financial applications), use java.math.BigDecimal. It has a variety of features designed to make it possible to parse and format numbers in any locale, including support for Western, Arabic, and Indic digits.

  5. #5
    jainsumit456 Guest

    Re: How to convert Double data type value into Integer one in Java

    You can parse your decimal value to int using
    int d=double.parseInt(7.25698);
    or you can use explicit conversion

    like

    double db=56.325;
    int in;

    in= int(db);

    i hope this will solve your query....

Similar Threads

  1. How to convert character data to integer data
    By FFCookie in forum Software Development
    Replies: 8
    Last Post: 26-09-2010, 03:28 AM
  2. Java BigDecimal to Integer
    By Anthony12 in forum Software Development
    Replies: 6
    Last Post: 11-08-2010, 05:09 PM
  3. Convert int to Integer type
    By Angelica Maria in forum Software Development
    Replies: 5
    Last Post: 12-03-2010, 11:59 AM
  4. conversion of Object into integer in JAVA
    By Jaisudha in forum Software Development
    Replies: 3
    Last Post: 14-11-2009, 12:45 PM
  5. Convert Integer to a String in Binary Notation
    By Naval in forum Software Development
    Replies: 2
    Last Post: 16-03-2009, 09:12 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,751,803,667.46185 seconds with 16 queries