• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Two Quick Java Questions

1: Is there a way to round to two decimal places without having to multiply by 100, Math.round, and then divide by 100?

2: If a value ends in .0, how can I have the program remove that from it?
 
1) Sure there is...no idea what, API has it!
2) if(value %1 == 0)
int tempValue = value;

I have no idea if that works but it seems pretty clever to me!
 
Ok, when I try that it doesn't really do anything because since 10.0 is a double and an int would be 10, they will never be the same. Heres the code:

intIncome = (int)varIncome;
if ((varIncome == intIncome);
{
varIncome = intIncome;
}

What am I doing wrong? Sorry if I sound like an idiot, but I'm new to Java
 
<---does not like Java, but will give it a shot

1. There is an API for it.

2.Assuming you already have a main

void SomeMethodName{

if (this.value%1!=0)
this.value=null;
else
this.value=this.value;<----can you do this?
}

keep in mind that io have no idea what your variables are.

edit: my bad, this should be a method, not a class.
 
just cheat, instead of doing this.value=this.value (not sure if you can do that), juts have it output something like "computed". That way the value will not change.
 
Originally posted by: silverpig
Can't you just do a parseInt(var)?

It's been a while since I took a java class...

That just makes it an int, but when the if statement sees if they are the same or not, to it 1.0 and 1 are not the same.
 
Originally posted by: Gibson486
just cheat, instead of doing this.value=this.value (not sure if you can do that), juts have it output something like "computed". That way the value will not change.

umm.... how about no
 
Just curious, why are you worrying about the format of the variable itself? I say leave the variable a float and only worry about formatting when you're actually displaying it.

float val = 1.0F;

if (val % 1 == 0) System.out.println((int)val);
else System.out.println(val);
 
Back
Top