|
From: <pat...@us...> - 2009-10-30 19:59:10
|
Revision: 975
http://cishell.svn.sourceforge.net/cishell/?rev=975&view=rev
Author: pataphil
Date: 2009-10-30 19:58:48 +0000 (Fri, 30 Oct 2009)
Log Message:
-----------
* Added NumberUtilities.convertToDecimalNotation (both versions).
* Note the TODO about updating the plot/csv converter plugin.
* Reviewed by Joseph.
Modified Paths:
--------------
trunk/core/org.cishell.utilities/src/org/cishell/utilities/NumberUtilities.java
Modified: trunk/core/org.cishell.utilities/src/org/cishell/utilities/NumberUtilities.java
===================================================================
--- trunk/core/org.cishell.utilities/src/org/cishell/utilities/NumberUtilities.java 2009-10-27 22:03:00 UTC (rev 974)
+++ trunk/core/org.cishell.utilities/src/org/cishell/utilities/NumberUtilities.java 2009-10-30 19:58:48 UTC (rev 975)
@@ -1,6 +1,13 @@
package org.cishell.utilities;
+import java.text.DecimalFormat;
+import java.text.Format;
+
public class NumberUtilities {
+ public static final String UNROUNDED_DECIMAL_PATTERN =
+ "#.############################";
+ public static final String NOT_A_NUMBER_PREFIX = "NOT A NUMBER";
+
public static Double interpretObjectAsDouble(Object object)
throws NumberFormatException {
final String EMPTY_CELL_MESSAGE = "An empty number cell was found.";
@@ -12,104 +19,84 @@
Number number = (Number)object;
return new Double(number.doubleValue());
- }
- else if (object instanceof short[]) {
+ } else if (object instanceof short[]) {
short[] objectAsShortArray = (short[])object;
if (objectAsShortArray.length == 0) {
throw new NumberFormatException(EMPTY_CELL_MESSAGE);
- }
- else {
+ } else {
return new Double(objectAsShortArray[0]);
}
- }
- else if (object instanceof Short[]) {
+ } else if (object instanceof Short[]) {
Short[] objectAsShortArray = (Short[])object;
if (objectAsShortArray.length == 0) {
throw new NumberFormatException(EMPTY_CELL_MESSAGE);
- }
- else {
+ } else {
return new Double(objectAsShortArray[0].doubleValue());
}
- }
- else if (object instanceof int[]) {
+ } else if (object instanceof int[]) {
int[] objectAsIntArray = (int[])object;
if (objectAsIntArray.length == 0) {
throw new NumberFormatException(EMPTY_CELL_MESSAGE);
- }
- else {
+ } else {
return new Double(objectAsIntArray[0]);
}
- }
- else if (object instanceof Integer[]) {
+ } else if (object instanceof Integer[]) {
Integer[] objectAsIntegerArray = (Integer[])object;
if (objectAsIntegerArray.length == 0) {
throw new NumberFormatException(EMPTY_CELL_MESSAGE);
- }
- else {
+ } else {
return new Double(objectAsIntegerArray[0].doubleValue());
}
- }
- else if (object instanceof long[]) {
+ } else if (object instanceof long[]) {
long[] objectAsLongArray = (long[])object;
if (objectAsLongArray.length == 0) {
throw new NumberFormatException(EMPTY_CELL_MESSAGE);
- }
- else {
+ } else {
return new Double(objectAsLongArray[0]);
}
- }
- else if (object instanceof Long[]) {
+ } else if (object instanceof Long[]) {
Long[] objectAsLongArray = (Long[])object;
if (objectAsLongArray.length == 0) {
throw new NumberFormatException(EMPTY_CELL_MESSAGE);
- }
- else {
+ } else {
return new Double(objectAsLongArray[0].doubleValue());
}
- }
- else if (object instanceof float[]) {
+ } else if (object instanceof float[]) {
float[] objectAsFloatArray = (float[])object;
if (objectAsFloatArray.length == 0) {
throw new NumberFormatException(EMPTY_CELL_MESSAGE);
- }
- else {
+ } else {
return new Double(objectAsFloatArray[0]);
}
- }
- else if (object instanceof Float[]) {
+ } else if (object instanceof Float[]) {
Float[] objectAsFloatArray = (Float[])object;
if (objectAsFloatArray.length == 0) {
throw new NumberFormatException(EMPTY_CELL_MESSAGE);
- }
- else {
+ } else {
return new Double(objectAsFloatArray[0].doubleValue());
}
- }
- else if (object instanceof double[]) {
+ } else if (object instanceof double[]) {
double[] objectAsDoubleArray = (double[])object;
if (objectAsDoubleArray.length == 0) {
throw new NumberFormatException(EMPTY_CELL_MESSAGE);
- }
- else {
+ } else {
return new Double(objectAsDoubleArray[0]);
}
- }
- else if (object instanceof Double[]) {
+ } else if (object instanceof Double[]) {
Double[] objectAsDoubleArray = (Double[])object;
if (objectAsDoubleArray.length == 0) {
throw new NumberFormatException(EMPTY_CELL_MESSAGE);
- }
- else {
+ } else {
return objectAsDoubleArray[0];
}
}
@@ -118,4 +105,31 @@
return new Double(objectAsString);
}
+
+ // TODO: Make the plot/csv converter use these versions.
+ public static String convertToDecimalNotation(double number) {
+ String numberAsString = new Double(number).toString();
+
+ return convertToDecimalNotation(numberAsString);
+ }
+
+ /*
+ * If numberAsString holds a number in scientific notation,
+ * convert it to decimal notation.
+ */
+ public static String convertToDecimalNotation(String numberAsString) {
+ // Check for a scientific notation delimiter.
+ if (numberAsString.contains("E") || numberAsString.contains("e")) {
+ Format format =
+ new DecimalFormat(UNROUNDED_DECIMAL_PATTERN);
+
+ try {
+ return format.format(new Double(numberAsString));
+ } catch (NumberFormatException numberFormatException) {
+ return NOT_A_NUMBER_PREFIX + " (" + numberAsString + ")";
+ }
+ } else {
+ return numberAsString;
+ }
+ }
}
\ No newline at end of file
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|