Home Java Java-bigdecimal Java bigdecimal shortValue example



Java bigdecimal shortValue example
Posted on: November 24, 2008 at 12:00 AM
In this example working of Java bigdecimal class shortValue method is demonstrated.

Java bigdecimal shortValue example

     

In this example working of  Java bigdecimal class shortValue method  is demonstrated.  Method return type is short therefore it will return only short values. Method converts bigdecimal in to short data type value. In the example one bigdecimal object  mass is created.   Method throws NumberFormatException if it finds any other value to be converted in to short  other than integers and decimals. Method also throws ArithmeticException if the value contains non-zero fractional part.

Syntax for using the method: public short shortValue() 

short sh = this.object.shortValue();

Java_bigdecimal_shortValue.java

import java.math.BigDecimal;

public class Java_bigdecimal_shortValue {
  public static void main(String args[]) {

  BigDecimal mass = new BigDecimal(2134);
  short shortValue;
  shortValue = mass.shortValueExact();
  System.out.println("BigDecimal value : " +
  "" + mass
  "\nmethod converted short value" +
  " : " + shortValue);

  float valuef = 3654;
  mass = new BigDecimal(valuef);
  System.out.println("\nmass object value : " +
  "" + mass + "\nmethod generated " +
  "short value : " +
  "" + mass.shortValue());

  double valueD = 1444;
  Double doubleObj = new Double(valueD);
  mass = new BigDecimal(doubleObj);
  System.out.println("\nmass object value :" +
  " " + mass  + "\nmethod generated " +
  "short value : " + mass.shortValue());

  }
}

Download the code

Related Tags for Java bigdecimal shortValue example:
javacexceptionclassormdataformobjectconvertiointegermethodformatfindtypedecimalgdvaluenumberreturnintthisactionaicreatearithmeticrowiftexforrowsexampleworkbigdecimalvaluestocontainsexamrefciwsshintegersbieilthrowitnumberformatrtezerolsdemoshortpeimartcedecimalsmateinnormpartasmntpartrjclesworkingembigmehrobjratexaxampsurnsocreatedatanyrackishaicellmplformatexceptioneaexceptandaractstrrtrtsvazssrithshoavstalufinlsojeigdpleplndonlyononlnon


More Tutorials from this section

Ask Questions?    Discuss: Java bigdecimal shortValue example  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.