The Artima Developer Community
Sponsored Link

Java Answers Forum
Dealing with Float

2 replies on 1 page. Most recent reply: Aug 14, 2003 12:30 AM by Rajendar

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Howard Owens

Posts: 1
Nickname: hbo3
Registered: Aug, 2003

Dealing with Float Posted: Aug 13, 2003 11:08 PM
Reply to this message Reply
Advertisement
I'm a newbie at Java.

As an exercise, I wanted to write a little application of my own imagination. I thought I would do something to calculate On Base Percentage (times reached base / plate appearances).

I'm having a hard time figuring out on to deal with the FLOAT output so that it comes out as something like .450 instead of some weird crap.

Any pointers would be appreciated.

Here is my latest attempt (of many) to get it:

import java.util.*;

public class CalcOBP {
int plateAppearances;
int reachBase;

public float CalcOBP(String inPlateAppearances, String inReachBase) {
float result = (Float(inReachBaseF) / Float(inPlateAppearancesF));
return (float)result;
}

public static void main(String[] arguments) {
CalcOBP cal = new CalcOBP();
float reachBase = Float.parseFloat(arguments[0]);
float plateAppearances = Float.parseFloat(arguments[1]);
System.out.println("This Batter's OBP is: " + cal);
}
}


Slager .

Posts: 16
Nickname: slager
Registered: May, 2003

Re: Dealing with Float Posted: Aug 14, 2003 12:28 AM
Reply to this message Reply
Take a look at java.text.DecimalFormat. Example is in the javadoc of the class.

Rajendar

Posts: 1
Nickname: rajendar
Registered: Aug, 2003

Re: Dealing with Float Posted: Aug 14, 2003 12:30 AM
Reply to this message Reply
Hi,
i think u can use in the NumberFormat class of java.text package for this.. u can limit the fraction digits to be displayed after the calculation..

ex:

public static String formatAmount (long lAmount)
{
NumberFormat aNumberFormat = NumberFormat.getInstance();
aNumberFormat.setGroupingUsed(true);
aNumberFormat.setMaximumFractionDigits(2);
aNumberFormat.setMinimumFractionDigits(2);
return aNumberFormat.format(lAmount);
}

-Raj

Flat View: This topic has 2 replies on 1 page
Topic: Ensuring single instance of app? Previous Topic   Next Topic Topic: WSAD and VSS

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use