How to Round a float to an nearest whole number

D

Don

I don't know what you mean by "other" uses for Average. The Average
declared in the quoted code has no uses at all, other than the extremely
suspicious assignment to itself.

The class Ch7Ex4 also has member fields called "average" and "Average",
but I don't see any use of DecimalFormat that gets anywhere near either
of those.

Patricia- Hide quoted text -

- Show quoted text -

And... I'm sorry about the "Average=Average;" part, I meant to use
that to try out "f" with Average=Average f; or something like that,
when it came up with an error I just erased the "f" and forgot about
the "Average=Average;"
Also, I meant to use have "doc.insertString(doc.getLength(), "\nThe
average of the grades is " + Average, textPane.getStyle("regular"));"
instead of the "doc.insertString(doc.getLength(), "\nThe average of
the grades is " + average, textPane.getStyle("regular"));" which I
believe I posted.
 
D

Don

Wahey! As complete fucking wankers go, this guy takes the week's biscuit!

...with considerable aplomb.

(He won't be reading this thread, and thus will never see my comments here --
which is fortunate, 'cos I would /hate/ to insult a beginner...)

-- chris

Please try to refrain from using offensive language. I wouldn't like
anyone's eyes to be hurt reading such content.
 
D

Don

Wahey! As complete fucking wankers go, this guy takes the week's biscuit!

...with considerable aplomb.

(He won't be reading this thread, and thus will never see my comments here --
which is fortunate, 'cos I would /hate/ to insult a beginner...)

-- chris

And don't worry about insulting me, you only caused me to believe that
you're a jagoff
 
P

Patricia Shanahan

Don said:
And... I'm sorry about the "Average=Average;" part, I meant to use
that to try out "f" with Average=Average f; or something like that,
when it came up with an error I just erased the "f" and forgot about
the "Average=Average;"
Also, I meant to use have "doc.insertString(doc.getLength(), "\nThe
average of the grades is " + Average, textPane.getStyle("regular"));"
instead of the "doc.insertString(doc.getLength(), "\nThe average of
the grades is " + average, textPane.getStyle("regular"));" which I
believe I posted.

In the code as posted, you have two different variables called
"Average". One an instance variable, declared in the line:

float average,gTotal,Average;

The other is a local variable declared:

float Average = Float.parseFloat(grade.format(average));

Its scope is from its declaration to the end of the block, inside
actionPerformed. Within its scope, the local variable "Average" hides
the instance field "Average".

My first interpretation of "Average = Average;" was that you meant to
have a separate local variable and assign it to either this.Average or
average. I think maybe you are not aware that you have two different
variables, and expect changes to Average in actionPerformed to operate
on the same Average as your display code.

At a higher level, I'm curious about why you chose to convert the String
result of format back to a float, rather than using it more directly in
the display. It has the undesirable side effect of displaying values
that are accurate to zero decimal places with one decimal place: 3.0
rather than just 3.

Patricia
 
D

Don

In the code as posted, you have two different variables called
"Average". One an instance variable, declared in the line:

float average,gTotal,Average;

The other is a local variable declared:

float Average = Float.parseFloat(grade.format(average));

Its scope is from its declaration to the end of the block, inside
actionPerformed. Within its scope, the local variable "Average" hides
the instance field "Average".

My first interpretation of "Average = Average;" was that you meant to
have a separate local variable and assign it to either this.Average or
average. I think maybe you are not aware that you have two different
variables, and expect changes to Average in actionPerformed to operate
on the same Average as your display code.

At a higher level, I'm curious about why you chose to convert the String
result of format back to a float, rather than using it more directly in
the display. It has the undesirable side effect of displaying values
that are accurate to zero decimal places with one decimal place: 3.0
rather than just 3.

Patricia- Hide quoted text -

- Show quoted text -

Oh, I didn't realize that that was why that was happening. I don't
exactly remember why I did that; I wrote that part a week or two ago
last time we had class. (My teachers went on strike for 7 days.)
I've changed the code now, I'll post it after I check it for more
mistakes and/or useless code.

By the way, is there any way to make 3.5 round up to 4 instead of 3,
or is that just how DecimalFormat formats?
 
P

Patricia Shanahan

Don wrote:
....
By the way, is there any way to make 3.5 round up to 4 instead of 3,
or is that just how DecimalFormat formats?

I don't really understand this question because 3.5 does round up to 4.
In general, x.5 will round to the even number, so 2.5 rounds to 2, and
both 3.5 and 4.5 round to 4.

You can get much more control over the rounding of a floating point
number by converting it to BigDecimal and setting the scale with a
specified rounding mode. You may want ROUND_HALF_UP, 'Rounding mode to
round towards "nearest neighbor" unless both neighbors are equidistant,
in which case round up.'

Patricia
 
D

Don

Don wrote:

...


I don't really understand this question because 3.5 does round up to 4.
In general, x.5 will round to the even number, so 2.5 rounds to 2, and
both 3.5 and 4.5 round to 4.

You can get much more control over the rounding of a floating point
number by converting it to BigDecimal and setting the scale with a
specified rounding mode. You may want ROUND_HALF_UP, 'Rounding mode to
round towards "nearest neighbor" unless both neighbors are equidistant,
in which case round up.'

Patricia

Oh, I was just using 3.5 as an example, I didn't actually try entering
it. So DecimalFormat will always round towards the even number?
Because the requirements for the program specifically said to use
DecimalFormat.
 
M

Mike Schilling

Don said:
Oh, I was just using 3.5 as an example, I didn't actually try entering
it. So DecimalFormat will always round towards the even number?
Because the requirements for the program specifically said to use
DecimalFormat.

The requirements for the program named a class to use. That's interesting.
Is this by any chance a school assignment?
 
P

Patricia Shanahan

Karl said:
[big snip]
By the way, is there any way to make 3.5 round up to 4 instead of 3,
or is that just how DecimalFormat formats?

Not to be a jerk or anything, but this is an extremely useful reference:
http://java.sun.com/javase/6/docs/api/index.html

and more specifically this:
http://java.sun.com/javase/6/docs/a....html#setRoundingMode(java.math.RoundingMode)

and this: http://java.sun.com/javase/6/docs/api/java/math/RoundingMode.html

That is nice! Unfortunately not available in 1.5 or earlier, but a much
more direct approach than converting to BigDecimal.

Patricia
 
M

murari garg

I need to round a float to the nearest whole number using
DecimalFormat (specific requirements for a program). I have tried
DecimalFormat grade = new DecimalFormat("0");
but it does nothing at all. What should I put in the parentheses to
just round to the nearest whole number?

class float {
public static void main(String[] args) {
float value = 12.33;
DecimalFormat grade = new DecimalFormat("0");
System.out.println(grade.format(value));
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top