Java BigDecimal valueOf()方法与示例
BigDecimal類的valueOf()方法 (BigDecimal Class valueOf() method)
Syntax:
句法:
public static BigDecimal valueOf (double d);public static BigDecimal valueOf (long l);public static BigDecimal valueOf (long unsc_val , int sc_val);valueOf() method is available in java.math package.
valueOf()方法在java.math包中可用。
valueOf (double d) method is used to convert the given double value into a BigDecimal.
valueOf(double d)方法用于將給定的double值轉換為BigDecimal。
valueOf (long l) method is used to convert the given long value into a BigDecimal.
valueOf(long l)方法用于將給定的long值轉換為BigDecimal。
valueOf (long unsc_val , int sc_val) method is used to convert the given long unscaled value and an integer value into a BigDecimal.
valueOf(long unsc_val,int sc_val)方法用于將給定的long非標度值和一個整數值轉換為BigDecimal。
These methods may throw an exception at the time of returning the value of the given parameter.
這些方法在返回給定參數的值時可能會引發異常。
NumberFormatException: This exception may throw when the given parameter is not finite.
NumberFormatException :如果給定參數不是有限的,則可能引發此異常。
These are non-static methods and it is accessible with class objects and if we try to access these methods with the class name then we will get an error.
這些是非靜態方法,可通過類對象訪問,如果嘗試使用類名訪問這些方法,則會收到錯誤消息。
Parameter(s):
參數:
In the first case, valueOf(double d),
在第一種情況下, valueOf(double d) ,
- double d – represents the double value to be converted to a BigDecimal.
- double d –表示要轉換為BigDecimal的double值。
In the first case, valueOf (long l),
在第一種情況下, valueOf(long l) ,
- long l – represents the long value to be converted to a BigInteger.
- long l –表示要轉換為BigInteger的long值。
In the first case, valueOf (long unsc_val, int sc_val),
在第一種情況下, valueOf(long unsc_val,int sc_val) ,
- long unsc_val – represents the unscaled value of this BigDecimal.
- long unsc_val –表示此BigDecimal的未縮放值。
- int sc_val – represents the scale of this BigDecimal.
- int sc_val –表示此BigDecimal的小數位數 。
Return value:
返回值:
In all the cases, the return type of the method is BigDecimal,
在所有情況下,方法的返回類型為BigDecimal 。
In the first case, it returns the converted double value to a BigDecimal.
在第一種情況下,它將轉換后的double值返回給BigDecimal。
In the second case, it returns the converted long value to a BigDecimal.
在第二種情況下,它將轉換后的long值返回給BigDecimal。
In the third case, it returns the BigDecimal and its value is calculated by using [(unsc_val) * 10 pow (-sc)].
在第三種情況下,它返回BigDecimal,并使用[[unsc_val)* 10 pow(-sc)]計算其值。
Example:
例:
// Java program to demonstrate the example // of valueOf() method of BigDecimalimport java.math.*;public class ValueOfOfBD {public static void main(String args[]) {// Instantiates three variables l_val// d_val, unscale_vallong l_val = 125487312456l;double d_val = 1245871.12345;long unscale_val = 123458745123l;// converts the given long value into// a BigDecimal and store it in a variable// named value_ofBigDecimal value_of = BigDecimal.valueOf(l_val);System.out.println("l_val: " + l_val);System.out.println("valueOf(long): ");// Display value_ofSystem.out.println("BigDecimal.valueOf(l_val): " + value_of);System.out.println();// converts the given double value into// a BigDecimal and store it in a variable// named value_ofvalue_of = BigDecimal.valueOf(d_val);System.out.println("d_val: " + d_val);System.out.println("valueOf(double): ");// Display value_ofSystem.out.println("BigDecimal.valueOf(d_val): " + value_of);System.out.println();// converts the given unscaled long value// with the given scale into a BigDecimal and// store it in a variable named value_ofvalue_of = BigDecimal.valueOf(unscale_val, 5);System.out.println("unscale_val: " + unscale_val);System.out.println("valueOf(long,int): ");// Display value_ofSystem.out.println("BigDecimal.valueOf(unscale_val,5): " + value_of);} }Output
輸出量
l_val: 125487312456 valueOf(long): BigDecimal.valueOf(l_val): 125487312456d_val: 1245871.12345 valueOf(double): BigDecimal.valueOf(d_val): 1245871.12345unscale_val: 123458745123 valueOf(long,int): BigDecimal.valueOf(unscale_val,5): 1234587.45123翻譯自: https://www.includehelp.com/java/bigdecimal-valueof-method-with-example.aspx
總結
以上是生活随笔為你收集整理的Java BigDecimal valueOf()方法与示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: julia在mac环境变量_在Julia
- 下一篇: inputstream示例_Java I