Solved

Change the method from Float to BigDecimal

Posted on 2012-03-26
12
381 Views
Last Modified: 2012-03-30
Hello,

I am new to Java and would like to you show me how to change the following java method c from Float to BigDecimal.  There have been errors with the tTotalTC figures.  The line of code in questions is:  f = Float.valueOf (to.getTaskOrdCombinedCost ());

Thanks for your help.

CODE BEGINS
 // Task Orders
        f=0;
        Iterator<FsTaskOrd> toIterator = toCollection.iterator();
        while (toIterator.hasNext ()) {
            FsTaskOrd to = toIterator.next();
            if (to != null) {
                if (ctrNam == null) {
                    ctrNam = to.getCtrNam ();
                }
                f=0;
                if (to.getTaskOrdQty()!=null) {
                    f = Float.valueOf (to.getTaskOrdQty ());
                }
                tTotalQ = tTotalQ + f;
               
                f=0;
                if ((to.getTaskOrdCombinedCost ()) != null) {
                    f = Float.valueOf (to.getTaskOrdCombinedCost ());
                }
                tTotalTC = tTotalTC + f;
            }
        } // End of while
CODE ENDS
0
Comment
Question by:capturetheflag
  • 5
  • 3
  • 3
  • +1
12 Comments
 
LVL 40

Expert Comment

by:gurvinder372
ID: 37765730
<<f=0>>

f is initialized to 0, but with 'what' data type?
0
 
LVL 1

Author Comment

by:capturetheflag
ID: 37765795
Hello,

Here are the data types.  Thanks

        //Task Ord Totals
        double tTotalQ = 0;
        double tTotalTC = 0;

        double f = 0;
        String strValue = "";
        String ctrNam = null;
0
 
LVL 1

Author Comment

by:capturetheflag
ID: 37765810
Hello,

I have also imported these packages.

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Collection;
import java.util.Iterator;

capturetheflag
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:CEHJ
ID: 37765857
There have been errors with the tTotalTC figures.

I notice there are still String types there - make sure they're not used in calculations.
Have you completely changed the code as i instructed in your last question about decimal errors?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37766844
0
 
LVL 1

Author Comment

by:capturetheflag
ID: 37771193
Hello,
 I am having trouble with this line of code.  i do not know how to change the code.
The number is coming in correctly but is converted incorrectly with this line of code.
What is the syntax?  Thanks for your help.

f = Float.valueOf (to.getTaskOrdCombinedCost ());
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37771233
Please, try to make a small self-consistent snippet of code which we cna try to compile and execute ourspeves.
Otherwise we don't have your classes and your types - it is really difficult to naswer your question.
Make a small class and try to recreate your problem with BigDecimal to float conversion.
This class can even help you resolve this issue yourslef.
If not , we can help you, but the snippet you quoted above has custom types for which there is no definition, so it is very difficult to help you.
0
 
LVL 1

Author Comment

by:capturetheflag
ID: 37771415
Hello for_van,

thanks for your help.  I have attached the jave files that I think you made need to review the code. Please let me know if you need anything else.  thanks
FsInvoice.txt
FsTaskOrder.txt
FundingSummaryData.txt
GetFundingSummaryAction.txt
TaskOrderData.txt
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37771464
Please, read the title of your question:

"change the method for float to BigDecimal"

If this is the question of how to change method from float to BigDecimal - do  we really need your whole project to address the issue?
If you analyze the point where you are having troubles, abstract it from all other details of your code
and make a short class where this problem is concentrated - it will help you to understand yourslef where is really your prooblem and it will help us relieveiung us from the necessity of going through all that code.
That is also the basic approach to solving any problems in code and even beyond - just analyze your issue, come down to individual points and isolate the places where the problems need to be addressed. You cannot study the whole thing every  time.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 250 total points
ID: 37771611
I've already told you what to do here:
http://www.experts-exchange.com/Programming/Languages/Java/Q_27644031.html#a37753329

    public String getTaskOrdCombinedCost () {
        return twoPlaces.format(this.taskOrdCombinedCost);
    }

Open in new window


should NOT return a String, but double:

 
public double getTaskOrdCombinedCost () {
        return taskOrdCombinedCost;
    }

Open in new window

0
 
LVL 1

Author Closing Comment

by:capturetheflag
ID: 37783594
Thanks,
for your help
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 37787074
:)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question