Solved

Adding 2 numbers using jstl 1.0

Posted on 2004-10-31
9,719 Views
Last Modified: 2013-11-24
Hi All,
    I have an property "Amount" of type BigDecimal in my value object, the VO is put into an ArrayList and kept in an Session object.

<c:set var="totAmt" value="0"/>

<c:forEach items"..." var="X">

// Adding the Amount inside the loop
<c:set var="totAmt" value="${totAmt + X.Amount}"/>

</c:ForEach>

<c:out value="totAmt"/>

When I print out the result, it displays only the integer whole numbers of the value.

Ex:
123.45 [Actual Amount Value]
123 [Displays]

Thanks in advance...
0
Question by:Tension
    6 Comments
     
    LVL 32

    Expert Comment

    by:ldbkutty
    Try with:
    <c:set var="totAmt" value="0.00"/>
    0
     
    LVL 21

    Expert Comment

    by:MogalManic
    Try initializing totAmt to 0.0.  Maybe JSTL is doing integer math.
    0
     
    LVL 2

    Expert Comment

    by:siliconeagle
    What type does X.getAmount() return int or float (or long or double). as long as there is a float there then JST should do floating point math. Check that X.getAmount() isn;t doing a round or otherwise converting to an integer.
    0
     

    Author Comment

    by:Tension
    Hi,
       I've tried initializing with
    <c:set var="totAmt" value="0.00"/>
    still it doen't work out.

    X.getAmount() is of type BigDecimal
    0
     
    LVL 2

    Accepted Solution

    by:
    there is a method setScale on BigDecimal maybe the getter should return a BigDecimal with a different scale.
    i.e.
    public double getAmount() {
        return amount.setScale(2).doubleValue();
    }
    0
     
    LVL 2

    Expert Comment

    by:siliconeagle
    or just:-
    public BigDecimal getAmount() {
        return amount.setScale(2);
    }
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
    Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
    The viewer will learn how to implement Singleton Design Pattern in Java.
    This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

    884 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

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now