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


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


描述

這個java.math.BigDecimal.scale()返回此 BigDecimal 的比例。如果為零或正數,則刻度是小數點右側的位數。

如果為負數,則數字的未換算值乘以 10 的取反乘冪。例如,比例為 -3 表示未縮放的值乘以 1000。

聲明

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

public int scale()

參數

NA

返回值

此方法返回此 BigDecimal 對象的比例。

異常

NA

示例

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

package com.tutorialspoint;

import java.math.*;

public class BigDecimalDemo {

   public static void main(String[] args) {

      // create 2 BigDecimal Objects
      BigDecimal bg1, bg2;

      bg1 = new BigDecimal("123.0");
      bg2 = new BigDecimal("-1.123");

      // create two int objects
      int i1,i2;

      // assign the result of scale on bg1, bg2 to i1,i2
      i1 = bg1.scale();
      i2 = bg2.scale();

      String str1 = "The scale of " + bg1 + " is " + i1;
      String str2 = "The scale of " + bg2 + " is " + i2;

      // print the values of i1,i2;
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

讓我們編譯並運行上麵的程序,這將產生以下結果——

The scale of 123.0 is 1
The scale of -1.123 is 3

相關用法


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