Crossfire JXClient, Trunk  R20561
Formatter.java
Go to the documentation of this file.
1 /*
2  * This file is part of JXClient, the Fullscreen Java Crossfire Client.
3  *
4  * JXClient is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * JXClient is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with JXClient; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  * Copyright (C) 2005-2008 Yann Chachkoff.
19  * Copyright (C) 2006-2011 Andreas Kirschbaum.
20  */
21 
22 package com.realtime.crossfire.jxclient.util;
23 
24 import org.jetbrains.annotations.NotNull;
25 
30 public class Formatter {
31 
35  private Formatter() {
36  }
37 
43  @NotNull
44  public static String formatLong(final long value) {
45  if (value < 1000000L) {
46  return Long.toString(value);
47  }
48 
49  if (value < 10000000L) {
50  final long tmp = (value+50000L)/100000L;
51  return tmp/10+"."+tmp%10+" million";
52  }
53 
54  if (value < 1000000000L) {
55  final long tmp = (value+500000L)/1000000L;
56  return tmp+" million";
57  }
58 
59  if (value < 10000000000L) {
60  final long tmp = (value+50000000L)/100000000L;
61  return tmp/10+"."+tmp%10+" billion";
62  }
63 
64  final long tmp = (value+500000000L)/1000000000L;
65  return tmp+" billion";
66  }
67 
75  @NotNull
76  public static String formatFloat(final double value, final int digits) {
77  final int tmp;
78  switch (digits) {
79  case 1:
80  tmp = (int)Math.round(value*10);
81  return tmp/10+"."+tmp%10;
82 
83  case 2:
84  tmp = (int)Math.round(value*100);
85  return tmp/100+"."+tmp/10%10+tmp%10;
86 
87  case 3:
88  tmp = (int)Math.round(value*1000);
89  return tmp/1000+"."+tmp/100%10+tmp/10%10+tmp%10;
90  }
91 
92  throw new IllegalArgumentException("invalid digits "+digits);
93  }
94 
95 }
static String formatLong(final long value)
Returns a.
Definition: Formatter.java:44
Utility class for formatting values into strings.
Definition: Formatter.java:30
Formatter()
Private constructor to prevent instantiation.
Definition: Formatter.java:35
static String formatFloat(final double value, final int digits)
Formats a float value for display.
Definition: Formatter.java:76