dmolony-DiskBrowser/src/com/bytezone/diskbrowser/visicalc/Format.java

95 lines
2.9 KiB
Java
Raw Normal View History

2017-02-25 03:56:22 +00:00
package com.bytezone.diskbrowser.visicalc;
public class Format
{
2017-03-12 16:47:11 +00:00
private static final String OVERFLOW = ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>";
private static final String HISTOGRAM = "***********************************";
private static final String UNKNOWN = "???????????????????????????????????";
2017-02-25 03:56:22 +00:00
2017-03-03 23:41:08 +00:00
private Format ()
{
}
2017-02-27 09:41:05 +00:00
static String format (Value value, char formatChar, int colWidth)
2017-02-25 03:56:22 +00:00
{
2017-02-28 20:39:26 +00:00
double actualValue = value.getValue ();
if (actualValue == -0.0)
actualValue = 0;
2017-02-25 03:56:22 +00:00
2017-02-28 20:39:26 +00:00
switch (formatChar)
2017-02-25 03:56:22 +00:00
{
2017-02-28 20:39:26 +00:00
case 'L':
case 'R':
2017-03-12 16:47:11 +00:00
case 'G':
2017-02-28 20:39:26 +00:00
case ' ':
2017-03-08 09:18:59 +00:00
String numberFormat = String.format ("%%%d.7f", colWidth + 8);
2017-02-28 20:39:26 +00:00
String val = String.format (numberFormat, actualValue);
2017-03-08 09:18:59 +00:00
val = val.trim ();
2017-02-28 20:39:26 +00:00
while (val.endsWith ("0"))
val = val.substring (0, val.length () - 1);
if (val.endsWith ("."))
val = val.substring (0, val.length () - 1);
if (val.startsWith ("0."))
val = val.substring (1);
2017-03-08 09:18:59 +00:00
if (val.length () > colWidth && val.indexOf ('.') >= 0)
val = val.substring (0, colWidth);
2017-02-28 20:39:26 +00:00
2017-03-08 09:18:59 +00:00
if (formatChar == 'L')
2017-02-28 20:39:26 +00:00
{
String leftFormat = String.format ("%%-%ds", colWidth);
2017-03-08 09:18:59 +00:00
val = String.format (leftFormat, val);
}
else
{
String rightFormat = String.format ("%%%ds", colWidth);
val = String.format (rightFormat, val);
2017-02-28 20:39:26 +00:00
}
2017-03-08 09:18:59 +00:00
if (val.length () > colWidth)
2017-03-12 16:47:11 +00:00
return OVERFLOW.substring (0, colWidth);
2017-03-08 09:18:59 +00:00
2017-02-28 20:39:26 +00:00
return val;
case 'I':
2017-03-12 16:47:11 +00:00
String integerFormat = String.format ("%%%dd", colWidth);
String result = String.format (integerFormat, (int) actualValue);
2017-02-28 20:39:26 +00:00
if (result.length () > colWidth)
2017-03-12 16:47:11 +00:00
return OVERFLOW.substring (0, colWidth);
2017-02-28 20:39:26 +00:00
return result;
case '$':
2017-03-12 16:47:11 +00:00
String currencyFormat = String.format ("%%%d.2f", colWidth + 3);
2017-03-08 09:18:59 +00:00
result = String.format (currencyFormat, actualValue).trim ();
String rightFormat = String.format ("%%%ds", colWidth);
val = String.format (rightFormat, result);
if (result.length () > colWidth)
2017-03-12 16:47:11 +00:00
return OVERFLOW.substring (0, colWidth);
2017-03-08 09:18:59 +00:00
return val;
2017-02-28 20:39:26 +00:00
case '*':
String graphFormat = String.format ("%%-%d.%ds", colWidth, colWidth);
2017-03-12 16:47:11 +00:00
return String.format (graphFormat, HISTOGRAM.substring (0, (int) actualValue));
2017-02-28 20:39:26 +00:00
default:
System.out.printf ("[%s]%n", formatChar);
2017-03-12 16:47:11 +00:00
return UNKNOWN.substring (0, colWidth);
2017-02-25 03:56:22 +00:00
}
}
2017-02-27 09:41:05 +00:00
static String justify (String text, int colWidth, char format)
2017-02-25 03:56:22 +00:00
{
// right justify
if (format == 'R' || format == '$' || format == 'I')
{
String labelFormat = String.format ("%%%d.%ds", colWidth, colWidth);
return (String.format (labelFormat, text));
}
// left justify
String labelFormat = String.format ("%%-%d.%ds", colWidth, colWidth);
return (String.format (labelFormat, text));
}
}