/** * Gets the integer index out of a SoyValue key, or throws SoyDataException if the key is not an * integer. * * @param key The SoyValue key. * @return The index. */ private int getIntegerIndex(SoyValue key) { try { return ((IntegerData) key).integerValue(); } catch (ClassCastException cce) { try { // TODO: Remove this old bad behavior after existing code is compliant. return Integer.parseInt(key.coerceToString()); } catch (NumberFormatException nfe) { throw new SoyDataException( "SoyList accessed with non-integer key (got key type " + key.getClass().getName() + ")."); } } } }
/** * Gets the integer index out of a SoyValue key, or throws SoyDataException if the key is not an * integer. * * @param key The SoyValue key. * @return The index. */ private int getIntegerIndex(SoyValue key) { try { return ((IntegerData) key).integerValue(); } catch (ClassCastException cce) { try { // TODO: Remove this old bad behavior after existing code is compliant. return Integer.parseInt(key.coerceToString()); } catch (NumberFormatException nfe) { throw new SoyDataException( "SoyList accessed with non-integer key (got key type " + key.getClass().getName() + ")."); } } } }
public static NumberData $$round( NumberData valueData, @Nullable IntegerData numDigitsAfterPtData) { int numDigitsAfterPt = (numDigitsAfterPtData != null) ? numDigitsAfterPtData.integerValue() : 0 /* default */; if (numDigitsAfterPt == 0) { if (valueData instanceof IntegerData) { return valueData; } else { return IntegerData.forValue((int) Math.round(valueData.numberValue())); } } else if (numDigitsAfterPt > 0) { double value = valueData.numberValue(); double shift = Math.pow(10, numDigitsAfterPt); return FloatData.forValue(Math.round(value * shift) / shift); } else { double value = valueData.numberValue(); double shift = Math.pow(10, -numDigitsAfterPt); return IntegerData.forValue((int) (Math.round(value / shift) * shift)); } }