Common Object data processing in Java

Source: Internet
Author: User

Converts an object to a double

public static Double changetyped (Object obj) {
try {
if (obj!=null) {
return new BigDecimal (double.valueof (obj + "")). Setscale (2,
BIGDECIMAL.ROUND_HALF_UP). Doublevalue ();
}else{
return 0.0;
}
} catch (Exception e) {
return 0.0;
}
}
Convert an object to an integer
public static Integer Changetypei (Object obj) {
try {
if (obj!=null) {
return Stringutils.tointeger (obj);
}else{
return 0;
}
} catch (Exception e) {
return 0;
}
}

Converts a string to an integer
public static String changetypes (Object obj) {
try {
return Stringutils.tointeger (obj). toString ();
} catch (Exception e) {
Return "";
}
}

/**
* Set the number 0 in map to null
*
* @param map
* @author: Nqy
* @Createtime: March 24, 2017 9:22:09
*/
public static void Setvalueisnull (map map) {
if (map! = null && map.size () > 0) {
Iterator it = Map.keyset (). Iterator ();
String key = null;

while (It.hasnext ()) {
Key = It.next (). toString ();

Object obj = map.get (key);
if (obj! = null) {
if (Numberutils.isnumber (obj.tostring ())) {
Double val = double.parsedouble (obj.tostring ());

if (val = = 0.0) {
Map.put (key, NULL);
}else if (val%1==0) {
Map.put (key, (int) Math.ceil (val));
}else if (val%1!=0) {
DecimalFormat df = new DecimalFormat ("#.00");
Map.put (Key, Df.format (Val));
}
}
}
}
}
}
/**
* Number of persons processed (single digit) divided by 10000
* @param obj
*/
public static String dealdouble (Object obj) {
DecimalFormat df = new DecimalFormat (dealreportutil.changetyped (obj) <10000? " 0.0000 ":" #.0000 ");
Return Df.format (dealreportutil.changetyped (obj)/10000);
}

Common Object data processing in Java

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.