Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,150,841 members, 7,810,245 topics. Date: Saturday, 27 April 2024 at 02:01 AM

Help! Double Data Type In Java Is Rounding Off My Result To .00 - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Help! Double Data Type In Java Is Rounding Off My Result To .00 (1892 Views)

5 Reasons Why Java Is The Best Programming Language / Best Data Type For Ip Address? / Community Service | I Am To Teach Everything I Know In Java For Free! (2) (3) (4)

(1) (Reply) (Go Down)

Help! Double Data Type In Java Is Rounding Off My Result To .00 by epospiky(m): 12:12pm On Nov 25, 2015
I tried writing a simple program which can calculate a couple of numbers entered. The result works fine only that that data type (double) which i declared the result to be in gives me a .00 value instead of maybe a .45 value or .86 value. It automatically rounds off the value which is kinda annoying. Anyone in the house who has an idea on this should help me out please.
Re: Help! Double Data Type In Java Is Rounding Off My Result To .00 by sunnico(m): 12:14pm On Nov 25, 2015
epospiky:
I tried writing a simple program which can calculate a couple of numbers entered. The result works fine only that that data type (double) which i declared the result to be in gives me a .00 value instead of maybe a .45 value or .86 value. It automatically rounds off the value which is kinda annoying. Anyone in the house who has an idea on this should help me out please.
Can u post your code also
Re: Help! Double Data Type In Java Is Rounding Off My Result To .00 by epospiky(m): 12:30pm On Nov 25, 2015
sunnico:

Can u post your code also

I am not with my pc right now. I will post the full code later in the evening if need be. Here is how i declared the method to be kind of.

poblic double setResult = ((data1*2)+(data2*3)+(data3*3)+(dat4*2)+(data5*2)/19
Re: Help! Double Data Type In Java Is Rounding Off My Result To .00 by sunnico(m): 1:57pm On Nov 25, 2015
i think the issue is with ur data1,data2..., did u define them as an integer type,
if yes u can either change thier data type to double or use a cast operator i.e
double setResult = double((data1*2)+(data2*3)+(data3*3)+(dat4*2)+(data5*2))/19;


*note public is used to reference variables outside the class not inside it, from the little i know
Re: Help! Double Data Type In Java Is Rounding Off My Result To .00 by khassy(m): 4:09pm On Nov 25, 2015
Cast it to int or long

.simple
Re: Help! Double Data Type In Java Is Rounding Off My Result To .00 by epospiky(m): 7:44pm On Nov 25, 2015
sunnico:
i think the issue is with ur data1,data2..., did u define them as an integer type,
if yes u can either change thier data type to double or use a cast operator i.e
double setResult = double((data1*2)+(data2*3)+(data3*3)+(dat4*2)+(data5*2))/19;


*note public is used to reference variables outside the class not inside it, from the little i know
yeah i defined them as integers kind of. I couldn't get string value directly from the textfield so i converted them from string to integer using Integer.parseInteger().
This is how i actually declared the result to be
public double setResult {
(return
(data1*2)+(data2*3)+(data3*3)+(dat4*2)+(data5**3))/19}
Re: Help! Double Data Type In Java Is Rounding Off My Result To .00 by sunnico(m): 9:33pm On Nov 25, 2015
epospiky:

yeah i defined them as integers kind of. I couldn't get string value directly from the textfield so i converted them from string to integer using Integer.parseInteger().
This is how i actually declared the result to be
public double setResult {
(return
(data1*2)+(data2*3)+(data3*3)+(dat4*2)+(data5**3))/19}
Okay, just use The cast operator as i said earlier dat should do it...

public double setResult {

return double((data1*2)+(data2*3)+(data3*3)+(dat4*2)+(data5*2))/19;}
Re: Help! Double Data Type In Java Is Rounding Off My Result To .00 by omohayek: 11:54pm On Nov 25, 2015
epospiky:


I am not with my pc right now. I will post the full code later in the evening if need be. Here is how i declared the method to be kind of.

poblic double setResult = ((data1*2)+(data2*3)+(data3*3)+(dat4*2)+(data5*2)/19

Your error is in using '/' to divide by 19, as in Java this will give a rounded result. Change 19 to 19.0 to force it to be a double.
Re: Help! Double Data Type In Java Is Rounding Off My Result To .00 by danvery2k6(m): 11:06pm On Nov 26, 2015
In addition to what the poster above me said, why are multiplying with integers?
Re: Help! Double Data Type In Java Is Rounding Off My Result To .00 by epospiky(m): 8:25pm On Nov 27, 2015
omohayek:


Your error is in using '/' to divide by 19, as in Java this will give a rounded result. Change 19 to 19.0 to force it to be a double.
Thanks bro. It really worked. But there is another problem. It is giving me too many long numbers after the decimal point. I want it to round off the number to exactly two decimal places. How can i achieve that?
Re: Help! Double Data Type In Java Is Rounding Off My Result To .00 by epospiky(m): 8:29pm On Nov 27, 2015
danvery2k6:
In addition to what the poster above me said, why are multiplying with integers?
yeah, i am multiplying the integers by those numbers in order to have specific values which i didn't declare at the beginning. Each data when entered is multiplied by the value of the textfield kinda.
Re: Help! Double Data Type In Java Is Rounding Off My Result To .00 by danvery2k6(m): 8:32pm On Nov 27, 2015
epospiky:

yeah, i am multiplying the integers by those numbers in order to have specific values which i didn't declare at the beginning. Each data when entered is multiplied by the value of the textfield kinda.

So Java does this thing called implicit type casting. When you try to perform arithmetic between another type and an Integer, the other type gets converted to an integer. its better you would have multiplied by doubles, say 2.0, 3.0, etc.
Re: Help! Double Data Type In Java Is Rounding Off My Result To .00 by epospiky(m): 8:53pm On Nov 27, 2015
danvery2k6:


So Java does this thing called implicit type casting. When you try to perform arithmetic between another type and an Integer, the other type gets converted to an integer. its better you would have multiplied by doubles, say 2.0, 3.0, etc.
uhmmm...i don't think there will be a problem if multiply them by integers(say 2,3 e.t.c). All datas entered are in integer data type except the result which of course i expect to be in double data type.
Re: Help! Double Data Type In Java Is Rounding Off My Result To .00 by omohayek: 11:19pm On Nov 27, 2015
epospiky:

Thanks bro. It really worked. But there is another problem. It is giving me too many long numbers after the decimal point. I want it to round off the number to exactly two decimal places. How can i achieve that?

Use the https://docs.oracle.com/javase/7/docs/api/java/text/DecimalFormat.html class - the API page has the details on how to do this.
Re: Help! Double Data Type In Java Is Rounding Off My Result To .00 by abenmariem: 12:19pm On Aug 02, 2016
I think that you have a casting issue.
I recommend also this website to learn about casting in java.
Re: Help! Double Data Type In Java Is Rounding Off My Result To .00 by snyperboi(m): 11:21pm On Aug 03, 2016
use printf to format the output when outputting the result e. g System.out.printf("%.2f", identifier); this outputs your result in two decimal places.

1 Like

(1) (Reply)

Android Programming: PC Is Not Intel Virtual Technology Enabled, What Can I Do? / Wrong Hustling / Top 10 Programming Languages To Learn

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 27
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.