2017-02-25 03:56:22 +00:00
|
|
|
package com.bytezone.diskbrowser.visicalc;
|
|
|
|
|
2020-02-10 11:05:40 +00:00
|
|
|
// -----------------------------------------------------------------------------------//
|
|
|
|
class Format
|
|
|
|
// -----------------------------------------------------------------------------------//
|
2017-02-25 03:56:22 +00:00
|
|
|
{
|
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
|
|
|
|
2020-02-10 11:05:40 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2017-03-03 23:41:08 +00:00
|
|
|
private Format ()
|
2020-02-10 11:05:40 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2017-03-03 23:41:08 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-02-10 11:05:40 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2017-02-27 09:41:05 +00:00
|
|
|
static String format (Value value, char formatChar, int colWidth)
|
2020-02-10 11:05:40 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2017-02-25 03:56:22 +00:00
|
|
|
{
|
2017-03-23 13:30:41 +00:00
|
|
|
double actualValue = value.getDouble ();
|
2017-02-28 20:39:26 +00:00
|
|
|
if (actualValue == -0.0)
|
|
|
|
actualValue = 0;
|
2017-02-25 03:56:22 +00:00
|
|
|
|
2017-03-13 07:16:20 +00:00
|
|
|
String valueText = String.valueOf ((int) actualValue);
|
|
|
|
if (valueText.startsWith ("0"))
|
|
|
|
valueText = valueText.substring (1);
|
|
|
|
int digits = valueText.length ();
|
|
|
|
if (digits > colWidth)
|
|
|
|
return OVERFLOW.substring (0, colWidth);
|
|
|
|
|
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-13 07:16:20 +00:00
|
|
|
int precision = colWidth - (digits + 1);
|
|
|
|
if (digits == 0)
|
|
|
|
precision = colWidth - 1;
|
|
|
|
if (precision < 0)
|
|
|
|
precision = 0;
|
|
|
|
String numberFormat = String.format ("%%%d.%df", colWidth, precision);
|
2017-02-28 20:39:26 +00:00
|
|
|
String val = String.format (numberFormat, actualValue);
|
2017-03-13 07:16:20 +00:00
|
|
|
// System.out.printf ("%s %2d %2d %s %15.8f %s : ", formatChar, colWidth,
|
|
|
|
// digits, numberFormat, actualValue, val);
|
2017-02-28 20:39:26 +00:00
|
|
|
|
2017-03-08 09:18:59 +00:00
|
|
|
val = val.trim ();
|
2017-03-13 07:16:20 +00:00
|
|
|
if (val.indexOf ('.') >= 0)
|
|
|
|
while (val.endsWith ("0"))
|
|
|
|
val = val.substring (0, val.length () - 1);
|
2017-02-28 20:39:26 +00:00
|
|
|
if (val.endsWith ("."))
|
|
|
|
val = val.substring (0, val.length () - 1);
|
|
|
|
if (val.startsWith ("0."))
|
|
|
|
val = val.substring (1);
|
2017-03-16 01:44:26 +00:00
|
|
|
if (val.startsWith ("-0."))
|
|
|
|
val = "-" + val.substring (2);
|
2017-02-28 20:39:26 +00:00
|
|
|
|
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-13 07:16:20 +00:00
|
|
|
String integerFormat = String.format ("%%%d.0f", colWidth);
|
|
|
|
String result = String.format (integerFormat, 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-10 11:05:40 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2017-02-27 09:41:05 +00:00
|
|
|
static String justify (String text, int colWidth, char format)
|
2020-02-10 11:05:40 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
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));
|
|
|
|
}
|
|
|
|
}
|