概述
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 BigDecimal
import java.math.*;
public class ValueOfOfBD {
public static void main(String args[]) {
// Instantiates three variables l_val
// d_val, unscale_val
long 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_of
BigDecimal value_of = BigDecimal.valueOf(l_val);
System.out.println("l_val: " + l_val);
System.out.println("valueOf(long): ");
// Display value_of
System.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_of
value_of = BigDecimal.valueOf(d_val);
System.out.println("d_val: " + d_val);
System.out.println("valueOf(double): ");
// Display value_of
System.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_of
value_of = BigDecimal.valueOf(unscale_val, 5);
System.out.println("unscale_val: " + unscale_val);
System.out.println("valueOf(long,int): ");
// Display value_of
System.out.println("BigDecimal.valueOf(unscale_val,5): " + value_of);
}
}
Output
输出量
l_val: 125487312456
valueOf(long):
BigDecimal.valueOf(l_val): 125487312456
d_val: 1245871.12345
valueOf(double):
BigDecimal.valueOf(d_val): 1245871.12345
unscale_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()方法与示例的全部内容,希望文章能够帮你解决Java BigDecimal valueOf()方法与示例所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复