當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java Java.math.BigDecimal.movePointLeft()用法及代碼示例


描述

這個java.math.BigDecimal.movePointLeft(int n)返回一個 BigDecimal,它與小數點向左移動 n 位等價。如果 n 為非負數,則調用僅將 n 添加到比例中。如果 n 為負,則調用等效於 movePointRight(-n)。

此調用返回的 BigDecimal 具有值 (this × 10-n) 和縮放最大值(this.scale()+n, 0)。

聲明

以下是聲明java.math.BigDecimal.movePointLeft()方法。

public BigDecimal movePointLeft(int n)

參數

n− 小數點向左移動的位數。

返回值

此方法返回一個 BigDecimal,它等同於小數點向左移動 n 位的小數點。

異常

ArithmeticException− 如果水垢溢出。

示例

下麵的例子展示了 math.BigDecimal.movePointLeft() 方法的用法。

package com.tutorialspoint;

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 right

      String 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.12323
After moving the Decimal point 12323 is 1232300

相關用法


注:本文由純淨天空篩選整理自 Java.math.BigDecimal.movePointLeft() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。