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


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


描述

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

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

聲明

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

public BigDecimal movePointRight(int n)

參數

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

返回值

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

異常

ArithmeticException− 如果水垢溢出。

示例

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

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.movePointRight(3); // 3 places right
      bg4 = bg2.movePointRight(-2);// 2 places left

      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 123230
After moving the Decimal point 12323 is 123.23

相關用法


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