Java.math.BigDecimal.movePointLeft()方法实例

来源:互联网 发布:电视盒子root软件 编辑:程序博客网 时间:2024/06/15 19:41

java.math.BigDecimal.movePointLeft(int n) 返回一个BigDecimal,它等效于将该值的小数点移动n位到左边。如果n为非负,则调用仅将n添加至刻度。如果n为负时,调用相当于调用movePointRight(-n)。

此调用返回的BigDecimal的值(this × 10-n) 和比例max(this.scale()+n, 0).

声明

以下是java.math.BigDecimal.movePointLeft()方法的声明

public BigDecimal movePointLeft(int n)

参数

  • n - 小数点向左移动位数

返回值

此方法返回一个BigDecimal,它等效于将该值的小数点向左移动n位

异常

  • ArithmeticException - 如果刻度溢出

例子

下面的例子显示math.BigDecimal.movePointLeft()方法的用法

package com.yiibai;import java.math.*;public class BigDecimalDemo {    public static void main(String[] args) {        // create 4 BigDecimal objects        BigDecimal bg1, bg2, bg3, bg4;        bg1 = new BigDecimal("123.23");        bg2 = new BigDecimal("12323");        bg3 = bg1.movePointLeft(3); // 3 points left        bg4 = bg2.movePointLeft(-2);// 2 points rightString str1 = "After moving the Decimal point " + bg1 + " is " + bg3;String str2 = "After moving the Decimal point " + bg2 + " is " + bg4;        // print bg3, bg4 values        System.out.println( str1 );        System.out.println( str2 );    }}

让我们编译和运行上面的程序,这将产生以下结果:

After moving the Decimal point 123.23 is 0.12323After moving the Decimal point 12323 is 1232300
0 0
原创粉丝点击