Crossfire JXClient, Trunk
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-2017,2019-2023 Andreas Kirschbaum
20  * Copyright (C) 2010-2012,2014-2018,2020-2023 Nicolas Weeger
21  */
22 
23 package com.realtime.crossfire.jxclient.util;
24 
25 import org.jetbrains.annotations.NotNull;
26 
31 public class Formatter {
32 
36  private Formatter() {
37  }
38 
44  @NotNull
45  public static String formatLong(final long value) {
46  if (value < 1000000L) {
47  return Long.toString(value);
48  }
49 
50  if (value < 10000000L) {
51  final long tmp = (value+50000L)/100000L;
52  return tmp/10+"."+tmp%10+" million";
53  }
54 
55  if (value < 1000000000L) {
56  final long tmp = (value+500000L)/1000000L;
57  return tmp+" million";
58  }
59 
60  if (value < 10000000000L) {
61  final long tmp = (value+50000000L)/100000000L;
62  return tmp/10+"."+tmp%10+" billion";
63  }
64 
65  final long tmp = (value+500000000L)/1000000000L;
66  return tmp+" billion";
67  }
68 
76  @NotNull
77  public static String formatFloat(final double value, final int digits) {
78  final int tmp;
79  switch (digits) {
80  case 1:
81  tmp = (int)Math.round(value*10);
82  return tmp/10+"."+tmp%10;
83 
84  case 2:
85  tmp = (int)Math.round(value*100);
86  return tmp/100+"."+tmp/10%10+tmp%10;
87 
88  case 3:
89  tmp = (int)Math.round(value*1000);
90  return tmp/1000+"."+tmp/100%10+tmp/10%10+tmp%10;
91  }
92 
93  throw new IllegalArgumentException("invalid digits "+digits);
94  }
95 
96 }
com.realtime.crossfire.jxclient.util.Formatter.formatFloat
static String formatFloat(final double value, final int digits)
Definition: Formatter.java:77
com.realtime.crossfire.jxclient.util.Formatter.formatLong
static String formatLong(final long value)
Definition: Formatter.java:45
com.realtime.crossfire.jxclient.util.Formatter
Definition: Formatter.java:31
com.realtime.crossfire.jxclient.util.Formatter.Formatter
Formatter()
Definition: Formatter.java:36