Java BigDecimal Tool Class

Source: Internet
Author: User
Tags mul

 Packagecom.core.calculate;ImportJava.math.BigDecimal;ImportJava.text.DecimalFormat;/*** Created by tangxin on 2017/1/6 0006. * If you have questions, please make corrections*/ Public classBignum { Public Static FinalString Big_num_fmt_comma = "#,###,###,###,###,###,# #0.00";//thousand separators for easy viewing of amount size     Public Static FinalString big_num_fmt = "################# #0.00";//without thousands separators     Public Static FinalString big_num_hundred = "100";//100 Constants     Public Static Final intBig_num_scale = 2;//keep two decimal places    Privatebignum () {}/*** High-precision addition *@paramV1 *@paramv2 *@return     */     Public StaticBigDecimal Add (String v1, String v2) {BigDecimal B1=NewBigDecimal (v1); BigDecimal B2=NewBigDecimal (v2); returnB1.add (B2); }    /*** High Precision subtraction *@paramV1 *@paramv2 *@return     */     Public StaticBigDecimal Sub (String v1,string v2) {BigDecimal B1=NewBigDecimal (v1); BigDecimal B2=NewBigDecimal (v2); returnb1.subtract (B2); }    /*** High precision multiplication *@paramV1 *@paramv2 *@return     */     Public StaticBigDecimal mul (String v1,string v2) {BigDecimal B1=NewBigDecimal (v1); BigDecimal B2=NewBigDecimal (v2); returnb1.multiply (B2); }    /*** High Precision Division *@paramV1 *@paramv2 *@paramScale *@return     */     Public StaticBigDecimal Div (String v1, String v2,intScale ) {        if(Scale < 0) {            Throw NewIllegalArgumentException ("The scale must is a positive integer or zero"); } BigDecimal B1=NewBigDecimal (v1); BigDecimal B2=NewBigDecimal (v2); returnb1.divide (B2, scale, bigdecimal.round_half_up); }    /*** Reserved decimal place *@paramv *@paramScale *@return     */     Public StaticBigDecimal Round (String V,intScale ) {        if(Scale < 0) {            Throw NewIllegalArgumentException ("The scale must is a positive integer or zero"); } BigDecimal b=NewBigDecimal (v); BigDecimal One=NewBigDecimal ("1"); returnb.divide (one, scale, bigdecimal.round_half_up); }    /*** Sub-conversion Narimoto *@paramv *@return     */     Public StaticBigDecimal penny2dollar (String v) {BigDecimal s= Div (V, "100", 2);//keep two decimal places        returns; }    /*** meta-conversion ingredient *@paramv *@return     */     Public StaticBigDecimal Dollar2penny (String v) {returnMul (V, "100"); }    /*** Formatted amount * Thousands separator convenient to view amount specific size big_num_fmt = "#,###,###,###,###,###,# #0.00" * Exact two decimal places. 0.99 * 1111111.985-1,111,111.99 *@paramv *@return     */     Public Staticstring FormatNumber (String v) {returnFormatNumber (V,big_num_fmt_comma); }    /*** Formatted amount *@paramv *@paramconstant BIG_NUM_FMT_COMMA,BIG_NUM_FMT in the pattern bignum class *@return     */     Public Staticstring FormatNumber (string v,string pattern) {return NewDecimalFormat (pattern). Format (new BigDecimal (v)); }     Public Static voidMain (string[] args) {System.out.println (Bignum.add ("1.99", "20.999")); System.out.println (Bignum.sub ("1.99", "20.999")); String s= "1111111.985"; String a=Bignum.formatnumber (s);        System.out.println (s);    System.out.println (a); }}

Java BigDecimal Tool Class

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.