42

HTML 5 で通貨をフォーマットすることに行き詰まっています。通貨をフォーマットする必要があるアプリケーションがあります。以下のコードスニペットがあります

 <td class="right"><span th:inline="text">$ [[${abc.value}]]</span></td>

DAO abc から通貨の値を読み取っているところは、フォーマットする必要があります。現在 $1200000.0 を印刷しています $1,200,000.0.0 を印刷する必要があります

4

4 に答える 4

72

#numbersここで確認できるユーティリティ オブジェクトを使用できます。

例えば:

<span th:inline="text">$ [[${#numbers.formatDecimal(abc.value, 0, 'COMMA', 2, 'POINT')}]]</span>

それにもかかわらず、インライン化せずにこれを行うこともできます (これは thymeleaf が推奨する方法です)。

<td>$ <span th:text="${#numbers.formatDecimal(abc.value, 0, 'COMMA', 2, 'POINT')}">10.00</span></td>
于 2013-01-04T22:15:29.410 に答える
25

アプリケーションが異なる言語を処理する必要がある場合は、 DEFAULT値 (= ロケールに基づく)を使用することをお勧めします。

${#numbers.formatDecimal(abc.value, 1, 'DEFAULT', 2, 'DEFAULT')}

Thymeleaf doc (より正確には NumberPointType )から:

/* 
 * Set minimum integer digits and thousands separator: 
 * 'POINT', 'COMMA', 'NONE' or 'DEFAULT' (by locale).
 * Also works with arrays, lists or sets
 */
${#numbers.formatInteger(num,3,'POINT')}
${#numbers.arrayFormatInteger(numArray,3,'POINT')}
${#numbers.listFormatInteger(numList,3,'POINT')}
${#numbers.setFormatInteger(numSet,3,'POINT')}

/*
 * Set minimum integer digits and (exact) decimal digits, and also decimal separator.
 * Also works with arrays, lists or sets
 */
${#numbers.formatDecimal(num,3,2,'COMMA')}
${#numbers.arrayFormatDecimal(numArray,3,2,'COMMA')}
${#numbers.listFormatDecimal(numList,3,2,'COMMA')}
${#numbers.setFormatDecimal(numSet,3,2,'COMMA')}
于 2015-05-13T19:09:26.687 に答える