Created
March 10, 2017 10:22
-
-
Save aldoKelvianto/d922cb739594da946ee57096996d44e7 to your computer and use it in GitHub Desktop.
for . delimiter countries (like Indonesia, Spanish, etc.)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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