Conversion Tool class between string, double, and long

Source: Internet
Author: User

 

Example

 

/** *  *  */public long getMoney_count_ms(){return this.money_count_ms;}/** *  *  */public void setMoney_count_ms(long money_count_ms){this.money_count_ms = money_count_ms;}public String getMoney_count_ms_str() {return NumberUtil.getLongToDouble(getMoney_count_ms());}public void setMoney_count_ms_str(String money_count_ms_str) {setMoney_count_ms(NumberUtil.setDoubleToLong(money_count_ms_str));}

 

 

Conversion Tool class between string, double, and long

 

/*** <PRE> * Conversion Tool class between string, double, and long ** @ comment: * 1, string --> double --> long * 2, long --> double --> long or double --> string ***/public class numberutil {public static double _ d_10000 = mongod; public static double _ d_100 = 100d; /*** <PRE> * string (30.58) to double (3058), * multiplied by 10000 (305800) *, and then converted to long (305800) * @ Param _ Str * @ return * </PRE> */public static long setdoubletolong (string _ Str) {string STR = numberutil. trim (_ Str); long _ long = 0; If ("". equals (STR) | "0 ". equals (STR) | "0.0 ". equals (STR) | "0.00 ". equals (STR) {_ long = 0;} else {double _ d = double. parsedouble (STR); _ long = (long) bigdecimalutil. mul (_ d, _ d_10000);} // system. out. println (_ long); Return _ long;}/*** <PRE> * long (305800,30000) converts double * by 10000 (30.58, 3). If no decimal point exists, convert it to long * and then convert it to string ** @ Param _ long * @ return * </PRE> */Public static string getlongtodouble (long _ long) {string _ STR = "0"; if (_ long = 0) {_ STR = "0 ";} else {double _ double = _ long; _ double = bigdecimalutil. div (_ double, _ d_10000); long _ = (long) _ long; _ long _ = _ long _/100l; _ long _ = _ long _/100l; if (_ double = _ long _) {_ STR = string. valueof (_ long _);} else {_ STR = string. valueof (_ double) ;}// system. out. println (_ Str); Return _ STR;}/*** test * @ par Am ARGs */public static void main (string [] ARGs) {numberutil. getlongtodouble (3000800); numberutil. setdoubletolong ("300.88");} public static string trim (string Str) {return (STR = NULL )? "": Str. Trim ());}}

 

Addition, subtraction, multiplication, and division of double type data

 

Import Java. math. bigdecimal;/*** <PRE> * Because Java's simple type cannot accurately perform floating-point operations, * this tool class provides precise floating-point operations, including addition, subtraction, multiplication, division, and rounding. * ** </PRE> */public class bigdecimalutil {/** default Division calculation precision */Private Static final int def_div_scale = 15; /** this class cannot be instantiated */private bigdecimalutil () {}/*** to provide precise addition operations. ** @ Param V1 * Add Number * @ Param V2 * Add Number * @ return and */public static double add (double V1, double V2) {bigdecimal b1 = new bigdecimal (double. tostring (V1); bigdecimal b2 = new bigdecimal (double. tostring (V2); Return b1.add (B2 ). doublevalue ();}/*** provides precise subtraction. ** @ Param V1 * subtrahend * @ Param V2 * subtrahend * @ Return Difference Between Two Parameters */public static double sub (double V1, double V2) {bigdecimal b1 = new bigdecimal (double. tostring (V1); bigdecimal b2 = new bigdecimal (double. tostring (V2); Return b1.subtract (B2 ). doublevalue ();}/*** provides exact multiplication. ** @ Param V1 * multiplier * @ Param V2 * multiplier * @ return product of two parameters */public static double MUL (double V1, double V2) {bigdecimal b1 = new bigdecimal (double. tostring (V1); bigdecimal b2 = new bigdecimal (double. tostring (V2); Return b1.multiply (B2 ). doublevalue ();}/*** provides (relatively) Precise Division operations. In case of Division, the Division is precise to 10 digits after the decimal point, and the subsequent digits are rounded down. ** @ Param V1 * divisor * @ Param V2 * divisor * @ return two parameters */public static double Div (double V1, double V2) {return Div (V1, v2, def_div_scale);}/*** provides (relatively) accurate Division operations. In case of division, the scale parameter determines the precision, and the subsequent numbers are rounded down. ** @ Param V1 * divisor * @ Param V2 * divisor * @ Param scale * indicates the number of digits after the decimal point. * @ Return operator of two parameters */public static double Div (double V1, double V2, int scale) {If (scale <0) {Throw new illegalargumentexception ("the scale must be a positive integer or zero");} bigdecimal b1 = new bigdecimal (double. tostring (V1); bigdecimal b2 = new bigdecimal (double. tostring (V2); Return b1.divide (B2, scale, bigdecimal. round_half_up ). doublevalue ();} public static void main (string [] ARGs) {double d = Bigdecimalutil. Div (3028800,100 00); system. Out. println (d);}/*** provides precise rounded decimal places. ** @ Param v * number to be rounded off * @ Param scale * number of digits to be retained after the decimal point * @ return result after rounding */public static 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, bigdecimal. round_half_up ). doublevalue ();}}

 

 

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.