@Test public void testFractionalDigits() throws Exception { DataRange rawRange = new DataRange(0, 100); DataRange unitsRange = new DataRange(0.25, 1.25); int fractionalDigits = 2; LinearScale ls = new LinearScale(rawRange, unitsRange, fractionalDigits); BigDecimal scaledValue = ls.scaleRawValue(0); Assert.assertTrue(BigDecimal.valueOf(0.25).setScale(fractionalDigits).compareTo(scaledValue) == 0); scaledValue = ls.scaleRawValue(50); Assert.assertTrue(BigDecimal.valueOf(0.75).setScale(fractionalDigits).compareTo(scaledValue) == 0); scaledValue = ls.scaleRawValue(100); Assert.assertTrue(BigDecimal.valueOf(1.25).setScale(fractionalDigits).compareTo(scaledValue) == 0); fractionalDigits = 1; ls = new LinearScale(rawRange, unitsRange, fractionalDigits); scaledValue = ls.scaleRawValue(0); Assert.assertTrue(BigDecimal.valueOf(0.3).setScale(fractionalDigits).compareTo(scaledValue) == 0); scaledValue = ls.scaleRawValue(50); Assert.assertTrue(BigDecimal.valueOf(0.8).setScale(fractionalDigits).compareTo(scaledValue) == 0); scaledValue = ls.scaleRawValue(100); Assert.assertTrue(BigDecimal.valueOf(1.3).setScale(fractionalDigits).compareTo(scaledValue) == 0); }
public BigDecimal scaleRawValue(int rawValue) throws EepOutOfRangeException { if(rawValue < minRawValue.intValue() || rawValue > maxRawValue.intValue()) { throw new EepOutOfRangeException( "Raw value ''{0}'' is out of valid range [{1}..{2}].", rawValue, minRawValue.intValue(), maxRawValue.intValue() ); } BigDecimal decRawValue = BigDecimal.valueOf(rawValue); BigDecimal scaledValue; int divideScale = Math.max(minUnitsValue.scale(), this.fractionalDigits); scaledValue = maxUnitsValue.subtract(minUnitsValue) .multiply( decRawValue.subtract(minRawValue)) .divide( maxRawValue.subtract(minRawValue), divideScale, RoundingMode.HALF_UP) .add( minUnitsValue) .setScale(this.fractionalDigits, RoundingMode.HALF_UP); return scaledValue; }
LinearScale { public BigDecimal scaleRawValue(int rawValue) throws EepOutOfRangeException { if(rawValue < minRawValue.intValue() || rawValue > maxRawValue.intValue()) { throw new EepOutOfRangeException( "Raw value ''{0}'' is out of valid range [{1}..{2}].", rawValue, minRawValue.intValue(), maxRawValue.intValue() ); } BigDecimal decRawValue = BigDecimal.valueOf(rawValue); BigDecimal scaledValue; int divideScale = Math.max(minUnitsValue.scale(), this.fractionalDigits); scaledValue = maxUnitsValue.subtract(minUnitsValue) .multiply( decRawValue.subtract(minRawValue)) .divide( maxRawValue.subtract(minRawValue), divideScale, RoundingMode.HALF_UP) .add( minUnitsValue) .setScale(this.fractionalDigits, RoundingMode.HALF_UP); return scaledValue; } }
LinearScale { public BigDecimal scaleRawValue(int rawValue) throws EepOutOfRangeException { if(rawValue < minRawValue.intValue() || rawValue > maxRawValue.intValue()) { throw new EepOutOfRangeException( "Raw value ''{0}'' is out of valid range [{1}..{2}].", rawValue, minRawValue.intValue(), maxRawValue.intValue() ); } BigDecimal decRawValue = BigDecimal.valueOf(rawValue); BigDecimal scaledValue; int divideScale = Math.max(minUnitsValue.scale(), this.fractionalDigits); scaledValue = maxUnitsValue.subtract(minUnitsValue) .multiply( decRawValue.subtract(minRawValue)) .divide( maxRawValue.subtract(minRawValue), divideScale, RoundingMode.HALF_UP) .add( minUnitsValue) .setScale(this.fractionalDigits, RoundingMode.HALF_UP); return scaledValue; } LinearScale(DataRange rawDataRange, DataRange unitsDataRange, int fractionalDigits); }
LinearScale { public BigDecimal scaleRawValue(int rawValue) throws EepOutOfRangeException { if(rawValue < minRawValue.intValue() || rawValue > maxRawValue.intValue()) { throw new EepOutOfRangeException( "Raw value ''{0}'' is out of valid range [{1}..{2}].", rawValue, minRawValue.intValue(), maxRawValue.intValue() ); } BigDecimal decRawValue = BigDecimal.valueOf(rawValue); BigDecimal scaledValue; int divideScale = Math.max(minUnitsValue.scale(), this.fractionalDigits); scaledValue = maxUnitsValue.subtract(minUnitsValue) .multiply( decRawValue.subtract(minRawValue)) .divide( maxRawValue.subtract(minRawValue), divideScale, RoundingMode.HALF_UP) .add( minUnitsValue) .setScale(this.fractionalDigits, RoundingMode.HALF_UP); return scaledValue; } LinearScale(DataRange rawDataRange, DataRange unitsDataRange, int fractionalDigits); BigDecimal scaleRawValue(int rawValue); }
LinearScale { public BigDecimal scaleRawValue(int rawValue) throws EepOutOfRangeException { if(rawValue < minRawValue.intValue() || rawValue > maxRawValue.intValue()) { throw new EepOutOfRangeException( "Raw value ''{0}'' is out of valid range [{1}..{2}].", rawValue, minRawValue.intValue(), maxRawValue.intValue() ); } BigDecimal decRawValue = BigDecimal.valueOf(rawValue); BigDecimal scaledValue; int divideScale = Math.max(minUnitsValue.scale(), this.fractionalDigits); scaledValue = maxUnitsValue.subtract(minUnitsValue) .multiply( decRawValue.subtract(minRawValue)) .divide( maxRawValue.subtract(minRawValue), divideScale, RoundingMode.HALF_UP) .add( minUnitsValue) .setScale(this.fractionalDigits, RoundingMode.HALF_UP); return scaledValue; } LinearScale(DataRange rawDataRange, DataRange unitsDataRange, int fractionalDigits); BigDecimal scaleRawValue(int rawValue);  }