DynFixedImplpublic class DynFixedImpl extends DynAnyBasicImpl implements DynFixed
Methods Summary |
---|
public java.lang.String | get_value()
if (status == STATUS_DESTROYED) {
throw wrapper.dynAnyDestroyed() ;
}
return any.extract_fixed().toString();
| public boolean | set_value(java.lang.String val)
if (status == STATUS_DESTROYED) {
throw wrapper.dynAnyDestroyed() ;
}
int digits = 0;
int scale = 0;
boolean preservedPrecision = true;
try {
digits = any.type().fixed_digits();
scale = any.type().fixed_scale();
} catch (BadKind ex) { // impossible
}
// First get rid of leading or trailing whitespace which is allowed
String string = val.trim();
if (string.length() == 0)
throw new TypeMismatch();
// Now scan for the sign
String sign = "";
if (string.charAt(0) == '-") {
sign = "-";
string = string.substring(1);
} else if (string.charAt(0) == '+") {
sign = "+";
string = string.substring(1);
}
// Now get rid of the letter d or D.
int dIndex = string.indexOf('d");
if (dIndex == -1) {
dIndex = string.indexOf('D");
}
if (dIndex != -1) {
string = string.substring(0, dIndex);
}
// Just to be sure
if (string.length() == 0)
throw new TypeMismatch();
// Now look for the dot to determine the integer part
String integerPart;
String fractionPart;
int currentScale;
int currentDigits;
int dotIndex = string.indexOf('.");
if (dotIndex == -1) {
integerPart = string;
fractionPart = null;
currentScale = 0;
currentDigits = integerPart.length();
} else if (dotIndex == 0 ) {
integerPart = null;
fractionPart = string;
currentScale = fractionPart.length();
currentDigits = currentScale;
} else {
integerPart = string.substring(0, dotIndex);
fractionPart = string.substring(dotIndex + 1);
currentScale = fractionPart.length();
currentDigits = integerPart.length() + currentScale;
}
// Let's see if we have to drop some precision
if (currentDigits > digits) {
preservedPrecision = false;
// truncate the fraction part
if (integerPart.length() < digits) {
fractionPart = fractionPart.substring(0, digits - integerPart.length());
} else if (integerPart.length() == digits) {
// currentScale > 0
// drop the fraction completely
fractionPart = null;
} else {
// integerPart.length() > digits
// unable to truncate fraction part
throw new InvalidValue();
}
}
// If val contains a value whose scale exceeds that of the DynFixed or is not initialized,
// the operation raises InvalidValue.
// Reinterpreted to mean raise InvalidValue only if the integer part exceeds precision,
// which is handled above (integerPart.length() > digits)
/*
if (currentScale > scale) {
throw new InvalidValue("Scale exceeds " + scale);
}
*/
// Now check whether both parts are valid numbers
BigDecimal result;
try {
new BigInteger(integerPart);
if (fractionPart == null) {
result = new BigDecimal(sign + integerPart);
} else {
new BigInteger(fractionPart);
result = new BigDecimal(sign + integerPart + "." + fractionPart);
}
} catch (NumberFormatException nfe) {
throw new TypeMismatch();
}
any.insert_fixed(result, any.type());
return preservedPrecision;
| public java.lang.String | toString()
int digits = 0;
int scale = 0;
try {
digits = any.type().fixed_digits();
scale = any.type().fixed_scale();
} catch (BadKind ex) { // impossible
}
return "DynFixed with value=" + this.get_value() + ", digits=" + digits + ", scale=" + scale;
|
|