-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEcgFormatNumber.java
51 lines (44 loc) · 1.56 KB
/
EcgFormatNumber.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
* EcgFormatNumber.java
*
* See EcgLicense.txt for License terms.
*/
/**
*
* @author Mauricio Villarroel ([email protected])
* Part of this code was taken from "somewhere" on the Internet.
*/
import java.text.DecimalFormat;
public class EcgFormatNumber {
final static DecimalFormat dec1 = new DecimalFormat("0.0");
final static DecimalFormat dec2 = new DecimalFormat("0.00");
final static DecimalFormat sci1 = new DecimalFormat("0.0E0");
final static DecimalFormat sci2 = new DecimalFormat("0.00E0");
/*
* Formats the 'number' parameter and returns it as a String.
* precision = number of decimal places in the output.
*/
public static String toString( double number,
double upLimit,
double loLimit,
int precision)
{
// If number less than decimalLimit, or equal to zero, use decimal style
if( number == 0.0 ||
(Math.abs(number) <= upLimit && Math.abs(number) > loLimit) )
{
switch (precision){
case 1 : return dec1.format(number);
case 2 : return dec2.format(number);
default: return dec1.format(number);
}
} else{
// Create the format for Scientific Notation with E
switch (precision){
case 1 : return sci1.format(number);
case 2 : return sci2.format(number);
default: return sci1.format(number);
}
}
}
}