Java.math.BigDecimal.scale() Method

Advertisements


Description

The java.math.BigDecimal.scale() returns the scale of this BigDecimal. If zero or positive, the scale is the number of digits to the right of the decimal point.

If negative, the unscaled value of the number is multiplied by ten to the power of the negation of the scale. For example, a scale of -3 means the unscaled value is multiplied by 1000.

Declaration

Following is the declaration for java.math.BigDecimal.scale() method

public int scale()

Parameters

  • NA

Return Value

This method returns the scale of this BigDecimal object

Exception

  • NA

Example

The following example shows the usage of math.BigDecimal.scale() method

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 );
    }
}

Let us compile and run the above program, this will produce the following result:

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



Advertisements
Advertisements