0

I have a class "Trade" with a property "initialPrice" of type BigDecimal. This property can have different of decimals depending on the property "decimals" contained in another class "Symbol" and therfore need different formats e.g. "#,###0.##" , "#,###0.#####" etc. This is no problem with output fields - I made a TagLib to solve that.

The problem is with the input field. Default is that i rounds of with 3 decimals so if you use more than 3 decmals you'll lose those when you update.

I don't know how or if it is even possible to use my TagLib here. I've been trying a lot of different ways but none have worked.

This my TagLib:

class PriceTagLib {
    def fmtPrice = {attrs, body->
        def BigDecimal number = attrs.number
        def int noOfDecimals = attrs.decimals
        switch (noOfDecimals) {
           case 1: out <<new DecimalFormat('###,##0.#').format(number)
            break
           case 2: out << new DecimalFormat('###,##0.##').format(number)
            break
           case 3: out << new DecimalFormat('###,##0.###').format(number)
            break
           case 4: out << new DecimalFormat('###,##0.####').format(number)
            break
           case 5: out << new DecimalFormat('###,##0.#####').format(number)
        }
    }
}  

Here's my classes...

class Symbol {
    String          name            //The name of the symbol e.g. EURUSD, USDCAD etc.
    int             decimals

    static hasMany = [trades:Trade]

}


class Trade {
    static belongsTo = [symbol:Symbol, strategy:Strategy]
    static hasMany = [positions:Position]

    BigDecimal      initialPrice
    Symbol          symbol
    Strategy        strategy
    Position        positions

    static constraints = {
    type(inList:["Sell", "Buy"])
        initialPrice(scale:5)
        positions(nullable:true)
    }

}

This from the show.gsp which works as I want:

<span class="property-value" aria-labelledby="initialPrice-label"><g:fmtPrice decimals="${tradeInstance.symbol.decimals}" number="${tradeInstance.initialPrice}"></g:fmtPrice></span>

Here is the line I need to modify - i.e. what I need to write between the quotes for the "value"-parameter. Maybee I need to replace the whole line? The line is in the _form.gsp template.

<g:field name="initialPrice" value="${tradeInstance.initialPrice}" required=""/>

Hope anyone could spread some light on this to help.

Thank's in advance...

larand
  • 773
  • 1
  • 9
  • 26

1 Answers1

1

you can simply call your tag lib within the value attribute:

<g:field name="initialPrice" value="${g.fmtPrice(decimals: tradeInstance.symbol.decimals, number: tradeInstance.initialPrice)}" required=""/>
hitty5
  • 1,653
  • 12
  • 25
  • Thank's that worked. I'm new, at least very unexperienced on webdevelopment, so I did not realized that I should replace g: with g. when called from the value-string. You know, I've already tried your solution and failed cause of the "g:" – larand Mar 27 '13 at 09:27