Guest User

Untitled

a guest
Nov 19th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. /**
  2. * Return string formatted with dot separator and prepended with currency
  3. * symbol (IDR). By default will return Rp 0.
  4. *
  5. * @param defaultOutput Default string to return (with currency prefix) when error happen.
  6. */
  7. @NonNull
  8. public static final String formatCurrency(@Nullable BigDecimal input, @NonNull String defaultOutput) {
  9. if (input == null)
  10. return defaultOutput;
  11. try {
  12. BigDecimal display = input.setScale(2, RoundingMode.HALF_EVEN);
  13. NumberFormat nf = NumberFormat.getInstance(new Locale("id", "ID"));
  14. nf.setMinimumFractionDigits(0);
  15. nf.setMaximumFractionDigits(2);
  16. return BuildConfig.CURRENCY_STR + nf.format(display.doubleValue());
  17.  
  18. } catch (Exception ex) {
  19. return defaultOutput;
  20. }
  21. }
  22.  
  23. /**
  24. * Return string formatted with dot separator, fixed 2 digits decimal, and prepended with
  25. * currency symbol (IDR). By default will return Rp 0.
  26. */
  27. @NonNull
  28. public static final String formatCurrency2Digits(@Nullable String strInput) {
  29. String defaultOutput = BuildConfig.CURRENCY_STR + "0";
  30.  
  31. if (strInput == null)
  32. return defaultOutput;
  33. try {
  34. BigDecimal bdInput = new BigDecimal(strInput);
  35. BigDecimal display = bdInput.setScale(2, RoundingMode.HALF_EVEN);
  36.  
  37. NumberFormat nf = NumberFormat.getInstance(new Locale("id", "ID"));
  38. nf.setMinimumFractionDigits(2);
  39. nf.setMaximumFractionDigits(2);
  40.  
  41. return BuildConfig.CURRENCY_STR + nf.format(display.doubleValue());
  42.  
  43. } catch (Exception ex) {
  44. return defaultOutput;
  45. }
  46. }
Add Comment
Please, Sign In to add comment