Here you can find the source of toBigDecimal(String s)
public static BigDecimal toBigDecimal(String s)
//package com.java2s; //License from project: Apache License import java.math.BigDecimal; import java.math.MathContext; import java.util.List; public class Main { public static final String EMPTY_STRING = ""; public static BigDecimal toBigDecimal(String s) { return toBigDecimal(s, null); }/* w w w .j av a2 s. c o m*/ public static BigDecimal toBigDecimal(String s, MathContext context) { if (isNullOrEmpty(s)) { return BigDecimal.ZERO; } s = s.replace(',', '.'); int lastDotPos = lastIndexOf(".", s); if (lastDotPos > -1) s = s.substring(0, lastDotPos).replace('.', ' ') + s.substring(lastDotPos); StringBuffer buf = new StringBuffer(); boolean colonFound = false; char[] exploded = removeSpaces(s).toCharArray(); for (int i = 0; i < exploded.length; i++) { if (Character.isDigit(exploded[i]) || ('.' == exploded[i]) || ('+' == exploded[i]) || ('-' == exploded[i])) { if ('.' == exploded[i]) { if (!colonFound) buf.append(exploded[i]); colonFound = true; } else { buf.append(exploded[i]); } } } String result = buf.toString().trim(); if (isNullOrEmpty(result)) return BigDecimal.ZERO; result = ".".equals(result) ? "0" : result; result = "+".equals(result) ? "0" : result; if (result.endsWith("-")) { result = result.substring(0, result.length() - 1) + "00"; } else if (result.indexOf('.') == -1) { result += ".00"; // append dots and zeros } else if (result.endsWith(".")) { result += "00"; // append zeros after dot } try { return (context == null) ? new BigDecimal(result) : new BigDecimal(result, context); } catch (NumberFormatException e) { System.err.println("Exception while parsing : " + s); e.printStackTrace(); return BigDecimal.ZERO; } } public static boolean isNullOrEmpty(String[] s) { if ((s == null) || (s.length == 0)) return true; for (int i = 0; i < s.length; i++) { if (!isNullOrEmpty(s[i])) { return false; } } return true; } public static boolean isNullOrEmpty(List<String> input) { return (input == null) ? true : isNullOrEmpty(input.toArray(new String[input.size()])); } public static boolean isNullOrEmpty(String s) { return (s == null) || s.equals(EMPTY_STRING); } public static int lastIndexOf(String pattern, String s) { if (!isNullOrEmpty(s)) { int pos = s.indexOf(pattern); while (pos != -1) { if ((pos + 1) >= s.length()) return pos; int newPos = s.substring(pos + 1).indexOf(pattern); if (newPos > 0) { pos = newPos + 1 + pos; } else { return pos; } } } return -1; } /** * Wirft aus einem String alle Spaces und Zeichen die kleiner sind raus. Wirkung ist mit trim * vergleichbar, allerdings wird der gesammte String bearbeitet. * * @param str String, der bearbeitet werden soll * * @return String ohne Spaces und */ public static String removeSpaces(String str) { if ((str == null) || (str.length() == 0)) { return str; } // erstmal losrennen, obs was zu tun gibt int i = 0; while ((i < str.length()) && (str.charAt(i) > ' ')) { i++; } // wenn i aufs Ende zeigt, gibst nix zu tun if (i == str.length()) { return str; } // Buffer fuers Ergebnis StringBuffer result = new StringBuffer(str.length() - 1); // es gibt was zu tun, also nicht doofe Zeichen umkopieren for (i = 0; i < str.length(); i++) { char c = str.charAt(i); if (c > ' ') { result.append(c); } } return result.toString(); } }