public class Test {
public String getAverage() {
int iSumOfGrades = 10;
int iNumberOfGrades = 3;
float dAverageGrade = 0;
dAverageGrade = iSumOfGrades/iNumberOfGrades;
return (new Float(dAverageGrade)).toString();
}
public static void main(String[] args) {
Test t = new Test();
System.out.println(t.getAverage());
}
}
Heather B:
>I'm stumped. How do I get this to return a string that contains a decimal
>value derived by dividing two integers? This is what I have and it returns
>"3.0" but I need "3.333".
>
>public class Test {
> public String getAverage() {
> int iSumOfGrades = 10;
> int iNumberOfGrades = 3;
> float dAverageGrade = 0;
> dAverageGrade = iSumOfGrades/iNumberOfGrades;
Here you're doing integer division. 10 / 3 is 3 if all you have are
integers. If you want this to be a float division, explicitly cast the
values to that type:
dAverageGrade = (float)iSumOfGrades/(float)iNumberOfGrades;
> return (new Float(dAverageGrade)).toString();
A simple
return Float.toString(dAverageGrade);
is enough here.
Check out the package java.text for more advanced formatting options.
Regards,
Marco
--
Please reply in the newsgroup, not by email!
Java programming tips: http://jiu.sourceforge.net/javatips.html
Other Java pages: http://www.geocities.com/marcoschmidt.geo/java.html
Best regards,
Todd
"Heather B" <heat...@glinberg.com> wrote in message
news:vmp2g0q...@corp.supernews.com...
>I'm stumped. How do I get this to return a string that contains a decimal
>value derived by dividing two integers? This is what I have and it returns
>"3.0" but I need "3.333".
See http://mindprod.com/jgloss/floatingpoint.html
--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
work in one, send in one... that just make more sence to me
andrew
I still wont mention the total paradox produced by the title "math in
java"
"Heather B" <heat...@glinberg.com> wrote in message news:<vmp2g0q...@corp.supernews.com>...
thedogfarted *
"thedogfarted" <thedog...@inbox.lv> wrote in message
news:bl1rb7$ten$1...@milzis.latnet.lv...
>2. are you sure the integer will be promoted to float in your example?
>should you not use
>dAverageGrade = (double) iSumOfGrades/(double)iNumberOfGrades;
>or is this redundant?
you only need one (double). Division automatically promotes the other
operand to match. Code generated is identical.
> 1. since double is just as fast or faster than float, use double.
Whether this is true or not depends on your hardware. I believe that on
some of the RISC type cpus float is significantly faster than double.
However the use of float should be restricted to those who have a good
understanding of floating point types.
Mark Thornton
Phil...
"Mark Thornton" <mark.p....@ntl-spam-world.com> wrote in message
news:lNbdb.913$QH3...@newsfep4-winn.server.ntli.net...
Date d0 = new Date();
long t0 = d0.getTime();
float f1 = 0.0f, f2 = 0.0f;
double f3 = 0.0;
for(long i=0; i<100000000; i++) {
f2 = (float) i * 17.0f;
}
Date d1 = new Date();
long t1 = d1.getTime();
for(long i=0; i<100000000; i++) {
f3 = (double) i * 17.0;
}
"Phil..." <ry...@ieee.org> wrote in message
news:pNhdb.163868$mp.8...@rwcrnsc51.ops.asp.att.net...
> Disagree do I.
> I ran a test and double was faster at multiply than fixed by a tiny bit
> (did not try other operations) despite the fact that my
> Celeron CPU has support for float but not double.
> Recall that 1.4.2 for MS is still interpreted.
>
> Phil...
The celeron is not one of the CPU that I was considering. Any method run
more than a few times in 1.4.2 (for Windows) will be compiled (unless
you have disabled the JIT).
The 1.4.2 server JVM will use SSE and SSE2 instructions where these are
available. SSE will improve float performance while SSE2 does the same
for double. In the absence of either of these (or when using client
compiler) the old FPU instructions are used. In this case the time taken
for float is very similar to that for double (and can be longer
depending on the costs of switching the accuracy mode).
Now try comparing float vs double performance on SPARC or Power processors.
Mark Thornton
>Disagree do I.
>I ran a test and double was faster at multiply than fixed by a tiny bit
>(did not try other operations) despite the fact that my
>Celeron CPU has support for float but not double.
>Recall that 1.4.2 for MS is still interpreted.
You don't disagree. The Celeron is not a RISC chip. In the Celeron,
the floating point works as a 64-bit double plus a few guard bits.
That is its native mode.
Now I want to fully understand you, are you saying that the
Celeron has only one kind of floating point and that it is 64 bit
but that it is considered Float rather than Double? Or that at
least the J2SE 1.4.2 jit uses is as a Float?
HTH, Phil... (I don't sound defensive do I?)
"Roedy Green" <ro...@seewebsite.com> wrote in message
news:2clbnv8tufnm67qb7...@4ax.com...