0

Although I defined the amount values as Double in Java file, these values seem to be typed as String in the exported Excel file. So, how can I force these values to be exported as Number instead of String? Is there a special method to be used ind sub.put(...), etc?

Java Class:

import java.io.Serializable;
import tr.com.cs.utility.Utility;

public class FinancePaymentList implements Serializable{

    private static final long serialVersionUID = 5987846930239863477L;  

    private String oid;
    private Double amount; 
    //... other properties      

    public void setAmountDue(Double amount) {
        this.amountDue = amountDue;
    }
}


JSON:

public static String jsonListToExcel (Collection<FinancePaymentList> list) throws TrnaspException {
    try {
        JSONObject obj = new JSONObject();
        JSONArray array = new JSONArray();
        for (FinancePaymentList form : list) {              
            JSONObject sub = new JSONObject();
            sub.put("oid", form.getOid());              
            sub.put("amount", form.getAmount());
            //... other properties
            array.put(sub);
        }
        obj.put("data", array);
        return obj.toString();
    } catch (Exception e) {
        throw new TrnaspException(e);
    }
}
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Jack
  • 1
  • 21
  • 118
  • 236
  • 4
    Use `double` with small `d`. This is the primitive numerical type. `Double` is a wrapper class holding a `double` value. – Joop Eggen Oct 05 '15 at 16:35
  • Please post sample json. This may have to do with how you create your excel file rather than java. How are you creating your final excel file? – Jags Oct 05 '15 at 16:40
  • 1
    Which JSON library are you using? – Nayuki Oct 05 '15 at 16:49
  • 1
    If you must use `Double` instead of `double`, then your `form.getAmount()` ,does it `return this.amountDue;`? If so, you might want to change that to `return this.amountDue.doubleValue();` as you'll be returning a `double` instead of a `Double`. Double likely has it's `.toString()` method called. Also, you'll want to check and see what `JSONObject.toString()` does in your JSON library documentation. – Arthur Weborg Oct 05 '15 at 17:00
  • @JoopEggen : Thanks for reply. As far as I see on [this](http://stackoverflow.com/questions/13332012/double-vs-double-in-java) post, I will need to use "d" after the double values. So, might it be enough to change just "Double" to "double" in the FinancePaymentList class? – Jack Oct 05 '15 at 18:27
  • @Jags Thanks, but I do not have a running server just now :( – Jack Oct 05 '15 at 18:28
  • 1
    @Christof how do you create excel file? – Jags Oct 05 '15 at 19:04
  • 1
    In general Double to double will do. Change `serialVersionUID` too to something else, and check that you do not use `null` as Double. Like in a database, keep fields NOT NULLABLE DEFAULT 0.0. – Joop Eggen Oct 05 '15 at 21:55
  • Many thanks for all of your comments and helps. I will try all the suggestions and I think they will solve the problem. Regards. – Jack Oct 06 '15 at 15:07

0 Answers0