BigDecimal longValue() in Java

longValue(): This method is available in java.math.BigDecimal class of Java.

Syntax:

long java.math.BigDecimal.longValue()

This method converts this BigDecimal to a long.

Parameters: NA

Returns: this BigDecimal converted to a long.

Exceptions: NA

Approach

Java

import java.math.BigDecimal;

public class BigDecimallongValue {
    public static void main(String[] args) {
        BigDecimal bigDecimal = new BigDecimal("1466.788");

        System.out.println(bigDecimal.longValue());
    }
}

Output:

1466


Some other methods of BigDecimal

BigDecimal.abs()This method returns a BigDecimal whose value is the absolute value of this BigDecimal, and whose scale is this.scale().

BigDecimal.abs(MathContext)This method returns a BigDecimal whose value is the absolute value of this BigDecimal, with rounding according to the context settings.

BigDecimal.add(BigDecimal) This method returns a BigDecimal whose value is (this +augend).

BigDecimal.add(BigDecimal, MathContext)This method returns a BigDecimal whose value is (this + augend), with rounding according to the context settings.

byteValue()This method returns the value of the specified number as a byte.

BigDecimal.byteValueExact()This method converts this BigDecimal to a byte, checking for lost information.

compareTo()This method compares this BigDecimal with the specified BigDecimal.

divide(BigDecimal)This method returns a BigDecimal whose value is (this /divisor).

divide(BigDecimal, MathContext)This method returns a BigDecimal whose value is (this /divisor), with rounding according to the context settings.

divide(BigDecimal, RoundingMode)This method returns a BigDecimal whose value is (this /divisor), and whose scale is this.scale().

divide(BigDecimal, int, RoundingMode)This method returns a BigDecimal whose value is (this /divisor), and whose scale is as specified.

divideAndRemainder(BigDecimal)This method returns a two-element BigDecimal array containing the result of divideToIntegralValue followed by the result of the remainder on the two operands.

divideAndRemainder(BigDecimal, MathContext)This method returns a two-element BigDecimal array containing the result of divideToIntegralValue followed by the result of the remainder on the two operands calculated with rounding according to the context settings.

divideToIntegralValue(BigDecimal)This method returns a BigDecimal whose value is the integer part of the quotient (this / divisor) rounded down.

divideToIntegralValue(BigDecimal ,MathContext)This method returns a BigDecimal whose value is the integer part of (this / divisor).

doubleValue()This method converts this BigDecimal to a double.

equals(Object)This method compares this BigDecimal with the specified Object for equality.

floatValue()This method converts this BigDecimal to a float.

hashCode()This method returns the hash code for this BigDecimal.

intValue()This method converts this BigDecimal to an int.

intValueExact()It converts this BigDecimal to an int, checking for lost information.

longValue(): It converts this BigDecimal to a long.

longValueExact()It converts this BigDecimal to a long, checking for lost information.

max() It returns the maximum of this BigDecimal and val.

min()It returns the minimum of this BigDecimal and val.

movePointLeft()It returns a BigDecimal which is equivalent to this one with the decimal point moved n places to the left.

movePointRight(): It returns a BigDecimal which is equivalent to this one with the decimal point moved n places to the right.

multiply(BigDecimal)It returns a BigDecimal whose value is (this ×multiplicand), and whose scale is (this.scale() +multiplicand.scale()).

multiply(BigDecimal, MathContext)It returns a BigDecimal whose value is (this ×multiplicand), with rounding according to the context settings.

negate()It returns a BigDecimal whose value is (-this),and whose scale is this.scale().

negate(MathContext)It returns a BigDecimal whose value is (-this), with rounding according to the context settings.

plus()It returns a BigDecimal whose value is (+this), and whose scale is this.scale().

plus(MathContext)It returns a BigDecimal whose value is (+this), with rounding according to the context settings.

pow(int)It returns a BigDecimal whose value is (this^n), The power is computed exactly, to unlimited precision.

pow(int, MathContext): It returns a BigDecimal whose value is (this%^n).

precision()It returns the precision of this BigDecimal.

remainder(BigDecimal)It returns a BigDecimal whose value is (this % divisor).

remainder(BigDecimal, MathContext)It returns a BigDecimal whose value is (this %divisor), with rounding according to the context settings.

round(MathContext)It returns a BigDecimal rounded according to the MathContext settings. 

scale()It returns the scale of this BigDecimal.

scaleByPowerOfTen(int) It returns a BigDecimal whose numerical value is equal to(this * 10^n).

setScale(int)It returns a BigDecimal whose scale is the specified value, and whose value is numerically equal to this BigDecimal's.

setScale(int, RoundingMode)It returns a BigDecimal whose scale is the specified value, and whose unscaled value is determined by multiplying or dividing this BigDecimal's unscaled value by the appropriate power of ten to maintain its overall value.

shortValue(): It returns the value of the specified number as a short.

shortValueExact()It converts this BigDecimal to a short, checking for lost information.

signum()It returns the signum function of this BigDecimal.

sqrt(MathContext)It returns an approximation to the square root of this with rounding according to the context settings.

stripTrailingZeros()It returns a BigDecimal which is numerically equal to this one but with any trailing zeros removed from the representation

subtract(BigDecimal) It returns a BigDecimal whose value is (this -subtrahend), and whose scale is max(this.scale(),subtrahend.scale()).

subtract(BigDecimal, MathContext): It returns a BigDecimal whose value is (this - subtrahend), with rounding according to the context settings.

Some constants of BigDecimal: Some constants of BigDecimal, BigDecimal.ONE, BigDecimal.ZERO and BigDecimal.TEN

toBigInteger()It converts this BigDecimal to a BigInteger.

toBigIntegerExact()It converts this BigDecimal to a BigInteger, checking for lost information. An exception is thrown if this BigDecimal has a nonzero fractional part.

toEngineeringString()It returns a string representation of this BigDecimal, using engineering notation if an exponent is needed.

toPlainString()It returns a string representation of this BigDecimal without an exponent field.

toString()It returns the string representation of this BigDecimal, using scientific notation if an exponent is needed.

ulp(): It returns the size of an ulp, a unit in the last place, of this BigDecimal.

unscaledValue()It returns a BigInteger whose value is the unscaled value of this BigDecimal. (Computes (this *10^this.scale()).)

BigDecimal.valueOf(long, int)It translates a long unscaled value and an int scale into a BigDecimal.

BigDecimal.valueOf(double): It translates a double into a BigDecimal, using the double's canonical string representation.

BigDecimal.valueOf(long)It translates a long value into a BigDecimalwith a scale of zero.

No comments:

Post a Comment