BrightSide Workbench Full Report + Source Code
DecimalFormats.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.elephant.util;
19 
20 import java.text.DecimalFormat;
21 import java.text.DecimalFormatSymbols;
22 import org.turro.elephant.context.Application;
23 
28 public class DecimalFormats {
29 
30  public static String format(Number value, String pattern) {
31  if(value == null) return null;
32  return new DecimalFormat(pattern, new DecimalFormatSymbols(Application.getUsedLocale())).format(value);
33  }
34 
35  public static String format(Number value) {
36  if(value == null) return null;
37  return DecimalFormat.getNumberInstance(Application.getUsedLocale()).format(value);
38  }
39 
40  public static String format(double value, String pattern) {
41  return new DecimalFormat(pattern, new DecimalFormatSymbols(Application.getUsedLocale())).format(value);
42  }
43 
44  public static String format(double value) {
45  return DecimalFormat.getNumberInstance(Application.getUsedLocale()).format(value);
46  }
47 
48  public static String format(long value, String pattern) {
49  return new DecimalFormat(pattern, new DecimalFormatSymbols(Application.getUsedLocale())).format(value);
50  }
51 
52  public static String format(long value) {
53  return DecimalFormat.getIntegerInstance(Application.getUsedLocale()).format(value);
54  }
55 
56  public static String formatCurrency(double value) {
57  return DecimalFormat.getCurrencyInstance(Application.getUsedLocale()).format(value);
58  }
59 
60  public static String formatPercent(double value) {
61  return DecimalFormat.getPercentInstance(Application.getUsedLocale()).format(value);
62  }
63 
64  public static String getStringFormat(int fractionDigits) {
65  StringBuilder sb = new StringBuilder("#,##0");
66  if(fractionDigits > 0) {
67  sb.append(".");
68  for(int i = 0; i < fractionDigits; i++) {
69  sb.append("0");
70  }
71  }
72  return sb.toString();
73  }
74 
75  private DecimalFormats() {
76  }
77 
78 }
static String format(Number value)
static String formatPercent(double value)
static String format(double value, String pattern)
static String format(double value)
static String format(Number value, String pattern)
static String format(long value, String pattern)
static String formatCurrency(double value)
static String getStringFormat(int fractionDigits)