Two Quick Java Questions

AgaBoogaBoo

Lifer
Feb 16, 2003
26,108
5
81
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?
 
 

MAME

Banned
Sep 19, 2003
9,281
1
0
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!
 

AgaBoogaBoo

Lifer
Feb 16, 2003
26,108
5
81
Originally posted by: Heisenberg
Originally posted by: AgaBooga
Originally posted by: Heisenberg
I like Starbucks' breakfast blend.

You know what???? Me too. Now answer the question regarding the programming language.
Hmm...maybe if it was in the correct forum I might. :p

Hehe, I needed a quick reply, not one tomorrow ;)
 

AgaBoogaBoo

Lifer
Feb 16, 2003
26,108
5
81
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
 

Gibson486

Lifer
Aug 9, 2000
18,378
2
0
<---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.
 

Spencer278

Diamond Member
Oct 11, 2002
3,637
0
0
There is some formating class or something that does both of the things you want. I can't remeber the name.
 

Gibson486

Lifer
Aug 9, 2000
18,378
2
0
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.
 

AgaBoogaBoo

Lifer
Feb 16, 2003
26,108
5
81
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.
 

AgaBoogaBoo

Lifer
Feb 16, 2003
26,108
5
81
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
 

lozina

Lifer
Sep 10, 2001
11,711
8
81
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);
 

AgaBoogaBoo

Lifer
Feb 16, 2003
26,108
5
81
Originally posted by: MAME
Everyone is doing the same thing I said....doesn't that work?

crap... I completely skipped over it, I'll give it a try :)

Thanks! And sorry about skipping over it!
 

MAME

Banned
Sep 19, 2003
9,281
1
0
Man, it was the first reply!

Anyway, just set the tempvalue to the newvalue if it works.