タイプ BigDecimal のプロパティ「initialPrice」を持つクラス「Trade」があります。このプロパティは、別のクラス「Symbol」に含まれるプロパティ「decimals」に応じて異なる小数を持つことができるため、「#,###0.##」、「#,###0.###」などの異なる形式が必要です。 ##" など。これは出力フィールドでは問題ありません。これを解決するために TagLib を作成しました。
問題は入力フィールドにあります。デフォルトでは、i は小数点以下 3 桁で四捨五入するため、3 つ以上の小数点以下を使用すると、更新時にそれらが失われます。
ここで TagLib を使用する方法、または可能かどうかさえわかりません。私は多くの異なる方法を試してきましたが、どれもうまくいきませんでした。
これは私の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)
}
}
}
これが私のクラスです...
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)
}
}
私が望むように動作するshow.gspからこれ:
<span class="property-value" aria-labelledby="initialPrice-label"><g:fmtPrice decimals="${tradeInstance.symbol.decimals}" number="${tradeInstance.initialPrice}"></g:fmtPrice></span>
変更する必要がある行は次のとおりです。つまり、「値」パラメーターの引用符の間に書く必要があるものです。たぶん、ライン全体を交換する必要がありますか?この行は _form.gsp テンプレートにあります。
<g:field name="initialPrice" value="${tradeInstance.initialPrice}" required=""/>
誰かがこれに光を当てて助けてくれることを願っています。
前もって感謝します...