The Artima Developer Community
Sponsored Link

Java Answers Forum
how do you change an integer into a double?

1 reply on 1 page. Most recent reply: Mar 6, 2006 5:13 AM by Matthias Neumair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Michelle Loh

Posts: 10
Nickname: modernage
Registered: Feb, 2006

how do you change an integer into a double? Posted: Mar 3, 2006 1:15 PM
Reply to this message Reply
Advertisement
Hi, if you were to divide an int numerator by an int denominator, like 1/4, and wanted the result to be in a decimal, .25, how would you turn it into a double?


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: how do you change an integer into a double? Posted: Mar 6, 2006 5:13 AM
Reply to this message Reply
You need to convert at least one of the numbers to a double value BEFORE the division.

The operator '/' means DIV, if both operators are integers ('%' would be the rest).
If one of the operators is a double, the other operator get's converted into double, too. After that, a floating point division will be executed.


If you use variables, this would be valid

double result = a / (double)b;


I don't know if this would work
double result = (double)a/b;

Use this form instead:
double result = ((double)a)/b;


If you use constats, you can also do this:
double result = 1 / 4.0;

or
double result = 1 / 4d;

Flat View: This topic has 1 reply on 1 page
Topic: can someone do this for me? Previous Topic   Next Topic Topic: need help in a program!!

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use