getFieldStringWithDec method
Implementation
String getFieldStringWithDec(int size, int decimalPlaces, num n) {
StringBuffer buffer = StringBuffer();
if (n != null) {
double dval = n.toDouble();
/* DecimalFormat documentation:
* NaN is formatted as a string, which typically has a single character \uFFFD.
* This string is determined by the DecimalFormatSymbols object.
* This is the only value for which the prefixes and suffixes are not used.
*
* Infinity is formatted as a string, which typically has a single character \u221E,
* with the positive or negative prefixes and suffixes applied.
* The infinity string is determined by the DecimalFormatSymbols object.
*/
/* However, the Double.toString method returns an ascii string, which is more ESRI-friendly */
if (dval.isNaN || dval.isInfinite) {
buffer.write(n.toString());
/* Should we use toString for integral numbers as well? */
} else {
var maxDig = decimalPlaces > 16 ? 16 : decimalPlaces;
var toAdd = decimalPlaces - 16;
numFormat.maximumFractionDigits = maxDig;
numFormat.minimumFractionDigits = decimalPlaces;
// FieldPosition fp = new FieldPosition(NumberFormat.FRACTION_FIELD);
// numFormat.format(n, buffer, fp);
String numStr = numFormat.format(n);
for (var i = 0; i < toAdd; i++) {
numStr += '0';
}
buffer.write(numStr);
// large-magnitude numbers may overflow the field size in non-exponent notation,
// so do a safety check and fall back to native representation to preserve value
// TODO check the impact of this, hope for testcase
// if (fp.getBeginIndex() >= size) {
// buffer.delete(0, buffer.length());
// buffer.append(n.toString());
// if (buffer.length() > size) {
// // we have a grevious problem -- the value does not fit in the required
// // size.
// ShpLogger().w(
// 'Writing DBF data, value $n cannot be represented in size $size');
// if (!swallowFieldSizeErrors) {
// // rather than truncate, and corrupt the data, we throw a Runtime
// throw ArgumentError(
// 'Value $n cannot be represented in size $size');
// }
// }
// }
}
}
String outBuf = buffer.toString();
int diff = size - buffer.length;
if (diff > 0) {
for (var i = 0; i < diff; i++) {
outBuf = ' ' + outBuf;
}
// buffer.insert(0, emptyString.substring(0, diff));
} else if (diff < 0) {
outBuf = outBuf.substring(0, size);
// buffer.setLength(size);
}
return outBuf;
}