Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 22-09-2009
Member
 
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.
Reply With Quote
  #2  
Old 22-09-2009
Member
 
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() );
Reply With Quote
  #3  
Old 22-09-2009
Member
 
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);
    }
}
Reply With Quote
  #4  
Old 22-09-2009
Member
 
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.
Reply With Quote
  #5  
Old 25-09-2009
jainsumit456
 
Posts: n/a
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....
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How to convert Double data type value into Integer one in Java"
Thread Thread Starter Forum Replies Last Post
How to convert character data to integer data FFCookie Software Development 8 26-09-2010 03:28 AM
Java BigDecimal to Integer Anthony12 Software Development 6 11-08-2010 05:09 PM
Convert int to Integer type Angelica Maria Software Development 5 12-03-2010 11:59 AM
conversion of Object into integer in JAVA Jaisudha Software Development 3 14-11-2009 12:45 PM
Convert Integer to a String in Binary Notation Naval Software Development 2 16-03-2009 09:12 PM


All times are GMT +5.5. The time now is 12:45 PM.