Data types are frequently encountered during Java development.

Source: Internet
Author: User

(1) when int or double is converted to BigDecimal, the number of digits increases accordingly. To solve this problem, you can round the long type of double by using the self-write function round.
After conversion, convert to String, and then use new BigDecimal () to convert
   
Example: fosum = new BigDecimal (String. & #118 alueOf (round (uo1sum. double & #118 alue () + uo2sum. double & #118 alue (), 3 )))
 
(2) convert Time to characters

Java. util. Date date = new java. util. Date (databean. getTyrq (). getTime ());
SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd ");
Tyrq = sdf. format (date );

(3) convert the string to the Timestamp type
Public java. SQL. Timestamp strToTimestamp (String str ){
Java. SQL. Timestamp sdate = null;

If (str! = Null ){

If (str. trim (). length () = 8 ){
Str = str. substring () + "-" + str. substring () + "-" + str. substring );

      }
Try {
DateFormat df = DateFormat. getDateInstance (DateFormat. DEFAULT, java. util. Locale. CHINA );
Java. util. Date date = df. parse (str );
Sdate = new java. SQL. Timestamp (date. getTime ());
} Catch (Exception e ){
E. printStackTrace ();
    }

}
Return sdate;
}
 
(4) rounding the double type
Public double round (double v, int scale ){

If (scale <0 ){

Throw new IllegalArgumentException ("The scale must be a positive integer or zero ");

       }

BigDecimal B = new BigDecimal (Double. toString (v ));

BigDecimal one = new BigDecimal ("1 ");

Return B. divide (one, scale,

<

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.