Skip to content

Instantly share code, notes, and snippets.

@aldoKelvianto
Created March 10, 2017 10:22
Show Gist options
  • Save aldoKelvianto/d922cb739594da946ee57096996d44e7 to your computer and use it in GitHub Desktop.
Save aldoKelvianto/d922cb739594da946ee57096996d44e7 to your computer and use it in GitHub Desktop.
for . delimiter countries (like Indonesia, Spanish, etc.)
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.util.Locale;
/**
* Created by aldo on 21/08/16.
*/
public class AutoFormatUtil {
private static final String FORMAT_NO_DECIMAL = "###,###";
private static final String FORMAT_WITH_DECIMAL = "###,###.###";
public static int getCharOccurance(String input, char c) {
int occurance = 0;
char[] chars = input.toCharArray();
for (char thisChar : chars) {
if (thisChar == c) {
occurance++;
}
}
return occurance;
}
public static String extractDigits(String input) {
return input.replaceAll("\\D+", "");
}
public static String formatToStringWithoutDecimal(double value) {
NumberFormat formatter = new DecimalFormat(FORMAT_NO_DECIMAL, getDecimalFormatSymbol());
return formatter.format(value);
}
public static DecimalFormatSymbols getDecimalFormatSymbol() {
Locale locale = new Locale("in");
return new DecimalFormatSymbols(locale);
}
public static String formatToStringWithoutDecimal(String value) {
return formatToStringWithoutDecimal(Double.parseDouble(value));
}
public static String formatWithDecimal(String price) {
return formatWithDecimal(Double.parseDouble(price));
}
public static String formatWithDecimal(double price) {
NumberFormat formatter = new DecimalFormat(FORMAT_WITH_DECIMAL, getDecimalFormatSymbol());
return formatter.format(price);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment