The Jakarta Project < Velocity Tools - Library >

Velocity Tools

Generic Tools

ツールドキュメント

その他のサブプロジェクト

MathTool リファレンスドキュメント
       

Tool for performing floating point math in Velocity.

Velocity で浮動少数点数演算を行うツールです。

A few things to note:

いくつかの注意点:

  • Most methods return numbers wrapped as Double which automatically render the decimal places even for whole numbers (e.g. new Double(1).toString() -> '1.0'). This is intentional. This tool is for floating point arithmetic. Integer arithmetic is already supported by the Velocity template language. If you really need '1' instead of '1.0', just call intValue() on the result.
  • No null pointer, number format, or divide by zero exceptions are thrown here. This is because such exceptions halt template rendering. It should be sufficient debugging feedback that Velocity will render the reference literally. (e.g. $math.div(1, 0) renders as '$math.div(1, 0)')
  • Class java.lang.Math is used to perform the mathematical operations.
  • ほとんどのメソッドは Double でラップされた数値を返却し、自動的に小数点まで表示されます (例: new Double(1).toString() -> '1.0')。 これは故意にそうしています。 このツールは浮動少数点数演算を目的としているからです。 整数の演算は既に Velocity テンプレート言語でサポートされています。 '1.0' ではなく '1' が必要なら、結果に対して intValue() を呼び出してください。
  • NullPointer, NumberFormat, DivideByZero 例外は送出されません。 これらの例外はテンプレートのレンダリングを停止させてしまうからです。 デバッグのためのフィードバックとして、Velocity はこれを文字通りそのまま表示します。 (例: $math.div(1, 0) は '$math.div(1, 0)' として表示されます)
  • 数値演算の処理には java.lang.Math クラスが使用されます。
Class
 org.apache.velocity.tools.generic.MathTool
Name
 $math (Velocityコンテキスト内での推奨名)
Toolbox設定例
 
<tool>
  <key>math</key>
  <scope>application</scope>
  <class>org.apache.velocity.tools.generic.MathTool</class>
</tool>
作者
 Nathan Bubna
Method Overview
add() Addition
sub() Subtraction
mul() Multiplication
div() Division
pow() Power of
max() Maximum of two numbers
min() Minimum of two numbers
abs() Absolute value of a number
toDouble() Converts a number into a double.
toInteger() Converts a number into an integer
roundTo() Rounds a number to the specified number of decimal places
roundToInt() Rounds a number to the nearest whole Integer
getRandom() Returns a pseudo-random number
random() Returns a pseudo-random number in a configurable range
Method Overview
add() 加算
sub() 減算
mul() 乗算
div() 除算
pow() 累乗
max() 二つの数値の最大値
min() 二つの数値の最小値
abs() 絶対値
toDouble() 数値を Double へ変換
toInteger() 数値を Integer へ変換
roundTo() 指定された小数点位置での数値の丸め処理
roundToInt() 一番近い整数値への数値の丸め処理
getRandom() 乱数値の返却
random() 指定された範囲の乱数の返却
add()
       

Addition

Double add(Object num1, Object num2)

Parameters
num1, num2
Operands of the addition. Valid input is any number (primitive types or objects, Velocity automatically converts primitives types to objects) or a string representation of a number.
Returns
A java.lang.Double representing the sum or null if the input paramters are not valid.

加算

Double add(Object num1, Object num2)

Parameters
num1, num2
加算する値。 有効な引数はあらゆる数値 (プリミティブ、もしくは Velocity が自動的にプリミティブに変換することのできるオブジェクト) 、または数値を表す String です。
Returns
合計を表す java.lang.Double 。 引数が不正の場合は null

sub()
       

Subtraction

Double sub(Object num1, Object num2)

Parameters
num1, num2
Operands of the subtraction. Valid input is any number (primitive types or objects, Velocity automatically converts primitives types to objects) or a string representation of a number.
Returns
A java.lang.Double representing the result of the subtraction or null if the input paramters are not valid.

減算

Double sub(Object num1, Object num2)

Parameters
num1, num2
減算する値。 有効な引数はあらゆる数値 (プリミティブ、もしくは Velocity が自動的にプリミティブに変換することのできるオブジェクト) 、または数値を表す String です。
Returns
減算の結果を表す java.lang.Double 。 引数が不正の場合は null

mul()
       

Multiplication

Double mul(Object num1, Object num2)

Parameters
num1, num2
Factors of the multiplication. Valid input is any number (primitive types or objects, Velocity automatically converts primitives types to objects) or a string representation of a number.
Returns
A java.lang.Double representing the result of the multiplication or null if the input paramters are not valid.

乗算

Double mul(Object num1, Object num2)

Parameters
num1, num2
乗算する値。 有効な引数はあらゆる数値 (プリミティブ、もしくは Velocity が自動的にプリミティブに変換することのできるオブジェクト) 、または数値を表す String です。
Returns
乗算の結果を表す java.lang.Double 。 引数が不正の場合は null

div()
       

Division

Double div(Object num1, Object num2)

Parameters
num1, num2
Input for the division. Valid input is any number (primitive types or objects, Velocity automatically converts primitives types to objects) or a string representation of a number.
Returns
A java.lang.Double representing the result of the division or null if the input paramters are not valid.

除算

Double div(Object num1, Object num2)

Parameters
num1, num2
除算の入力値。 有効な引数はあらゆる数値 (プリミティブ、もしくは Velocity が自動的にプリミティブに変換することのできるオブジェクト) 、または数値を表す String です。
Returns
除算の結果を表す java.lang.Double 。 引数が不正の場合は null

pow()
       

Power of

Double pow(Object num1, Object num2)

Parameters
num1, num2
Operands. Valid input is any number (primitive types or objects, Velocity automatically converts primitives types to objects) or a string representation of a number.
Returns
A java.lang.Double representing the first number raised to the power of the second or null if the input paramters are not valid.

累乗

Double pow(Object num1, Object num2)

Parameters
num1, num2
演算値。 有効な引数はあらゆる数値 (プリミティブ、もしくは Velocity が自動的にプリミティブに変換することのできるオブジェクト) 、または数値を表す String です。
Returns
一番目の数値を二番目の数値で累乗した結果を表す java.lang.Double 。 引数が不正の場合は null

max()
       

Maximum of two numbers

Double max(Object num1, Object num2)

Parameters
num1, num2
Operands. Valid input is any number (primitive types or objects, Velocity automatically converts primitives types to objects) or a string representation of a number.
Returns
A java.lang.Double representing the maximum of the two numbers or null if the input paramters are not valid.

二つの数値の最大値

Double max(Object num1, Object num2)

Parameters
num1, num2
演算値。 有効な引数はあらゆる数値 (プリミティブ、もしくは Velocity が自動的にプリミティブに変換することのできるオブジェクト) 、または数値を表す String です。
Returns
2つの数値の大きい方を表す java.lang.Double 。 引数が不正の場合は null

min()
       

Minimum of two numbers

Double min(Object num1, Object num2)

Parameters
num1, num2
Operands. Valid input is any number (primitive types or objects, Velocity automatically converts primitives types to objects) or a string representation of a number.
Returns
A java.lang.Double representing the minimum of the two numbers or null if the input paramters are not valid.

二つの数値の最小値

Double min(Object num1, Object num2)

Parameters
num1, num2
演算値。 有効な引数はあらゆる数値 (プリミティブ、もしくは Velocity が自動的にプリミティブに変換することのできるオブジェクト) 、または数値を表す String です。
Returns
2つの数値の小さい方を表す java.lang.Double 。 引数が不正の場合は null

abs()
       

Absolute value of a number

Double abs(Object num)

Parameters
num
Operand. Valid input is any number (primitive types or objects, Velocity automatically converts primitives types to objects) or a string representation of a number.
Returns
A java.lang.Double representing the absolute value of the input or null if the input paramter is not valid.

絶対値

Double abs(Object num)

Parameters
num
演算値。 有効な引数はあらゆる数値 (プリミティブ、もしくは Velocity が自動的にプリミティブに変換することのできるオブジェクト) 、または数値を表す String です。
Returns
絶対値を表す java.lang.Double 。 引数が不正の場合は null

toDouble()
       

Converts a number into a double.

Double toDouble(Object num)

Parameters
num
Operand. Valid input is any number (primitive types or objects, Velocity automatically converts primitives types to objects) or a string representation of a number.
Returns
A java.lang.Double representing the input number or null if the input paramter is not valid.

数値を Double へ変換

Double toDouble(Object num)

Parameters
num
演算値。 有効な引数はあらゆる数値 (プリミティブ、もしくは Velocity が自動的にプリミティブに変換することのできるオブジェクト) 、または数値を表す String です。
Returns
入力値を表す java.lang.Double 。 引数が不正の場合は null

toInteger()
       

Converts a number into an integer

Integer toInteger(Object num)

Parameters
num
Operand. Valid input is any number (primitive types or objects, Velocity automatically converts primitives types to objects) or a string representation of a number.
Returns
A java.lang.Integer representing the input number or null if the input paramter is not valid.

数値を Integer へ変換

Integer toInteger(Object num)

Parameters
num
演算値。 有効な引数はあらゆる数値 (プリミティブ、もしくは Velocity が自動的にプリミティブに変換することのできるオブジェクト) 、または数値を表す String です。
Returns
入力値を表す java.lang.Integer 。 引数が不正の場合は null

roundTo()
       

Rounds a number to the specified number of decimal places

Double roundTo(Object decimals, Object num)

Parameters
decimals
The number of decimal places. Valid input is any number (primitive types or objects, Velocity automatically converts primitives types to objects) or a string representation of a number.
num
The number to round. Valid input is any number (primitive types or objects, Velocity automatically converts primitives types to objects) or a string representation of a number.
Returns
A java.lang.Double representing the input number rounded to the specified number of decimal places or null if the input paramter is not valid.

This method is particulary useful for simple display formatting. If you want to round an number to the nearest integer, it is better to use method roundToInt(), as that will return an java.lang.Integer rather than a java.lang.Double.


指定された小数点位置での数値の丸め処理

Double roundTo(Object decimals, Object num)

Parameters
decimals
小数点位置。 有効な引数はあらゆる数値 (プリミティブ、もしくは Velocity が自動的にプリミティブに変換することのできるオブジェクト) 、または数値を表す String です。
num
丸める数値。 有効な引数はあらゆる数値 (プリミティブ、もしくは Velocity が自動的にプリミティブに変換することのできるオブジェクト) 、または数値を表す String です。
Returns
渡された数値を指定された小数点位置で丸めた java.lang.Double 。 引数が不正の場合は null

このメソッドはシンプルな表示フォーマットを行うのに役立ちます。 最も近い整数値に丸めたい場合は java.lang.Double ではなく java.lang.Integer を返却する roundToInt() メソッドを使用するほうが良いでしょう。


roundToInt()
       

Rounds a number to the nearest whole Integer

Integer roundToInt(Object num)

Parameters
num
The number to round. Valid input is any number (primitive types or objects, Velocity automatically converts primitives types to objects) or a string representation of a number.
Returns
A java.lang.Integer representing the input number rounded to nearest whole Integer or null if the input paramter is not valid.

一番近い整数値への数値の丸め処理

Integer roundToInt(Object num)

Parameters
num
丸める数値。 有効な引数はあらゆる数値 (プリミティブ、もしくは Velocity が自動的にプリミティブに変換することのできるオブジェクト) 、または数値を表す String です。
Returns
一番近い整数値へと丸めた java.lang.Integer 。 引数が不正の場合は null

getRandom()
       

Returns a pseudo-random number

Double getRandom()

Returns
A java.lang.Double greater than or equal to 0.0 and less than 1.0.

乱数値の返却

Double getRandom()

Returns
0.0 以上 1.0 未満の java.lang.Double

random()
       

Returns a pseudo-random number in a configurable range

Integer random(Object num1, Object num2)

Parameters
num1, num2
First and last number of range. Valid input is any number (primitive types or objects, Velocity automatically converts primitives types to objects) or a string representation of a number.
Returns
A java.lang.Integer greater than or equal to the first number and less than the second number or null if the input paramter is not valid.

指定された範囲の乱数の返却

Integer random(Object num1, Object num2)

Parameters
num1, num2
範囲の開始値と終了値。 有効な引数はあらゆる数値 (プリミティブ、もしくは Velocity が自動的にプリミティブに変換することのできるオブジェクト) 、または数値を表す String です。
Returns
一番目の数値 以上 二番目の数値 未満の java.lang.Integer 。 引数が不正の場合は null


[訳注: この文書は 木村 貴由 高橋 達男 が翻訳しました。 日本語訳に対するコメントがあれば、report@jajakarta.orgに送って下さい。]
Copyright © 1999-2003, Apache Software Foundation