java.math.BigDecimal.floatValue()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(6.6k)|赞(0)|评价(0)|浏览(153)

本文整理了Java中java.math.BigDecimal.floatValue()方法的一些代码示例,展示了BigDecimal.floatValue()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。BigDecimal.floatValue()方法的具体详情如下:
包路径:java.math.BigDecimal
类名称:BigDecimal
方法名:floatValue

BigDecimal.floatValue介绍

[英]Returns this BigDecimal as a float value. If this is too big to be represented as an float, then Float.POSITIVE_INFINITYor Float.NEGATIVE_INFINITY is returned.

Note, that if the unscaled value has more than 24 significant digits, then this decimal cannot be represented exactly in a float variable. In this case the result is rounded.

For example, if the instance x1 = new BigDecimal("0.1") cannot be represented exactly as a float, and thus x1.equals(new returns false for this case.

Similarly, if the instance new BigDecimal(16777217) is converted to a float, the result is 1.6777216E7.
[中]将此BigDecimal作为浮点值返回。如果它太大而无法表示为浮点,则为浮点。正无穷大或浮点数。返回负无穷大。
请注意,如果未标度值的有效位数超过24位,则无法在浮点变量中准确表示该十进制数。在这种情况下,结果是四舍五入的。
例如,如果实例x1=new BigDecimal(“0.1”)不能精确地表示为浮点,则x1。equals(new)在这种情况下返回false。
类似地,如果实例new BigDecimal(16777217)转换为浮点,则结果为1.6777216E7。

代码示例

代码示例来源:origin: prestodb/presto

@Override
public float floatValue() { return _value.floatValue(); }

代码示例来源:origin: redisson/redisson

@Override
public float floatValue() { return _value.floatValue(); }

代码示例来源:origin: openhab/openhab1-addons

@Override
public float floatValue() {
  return value.floatValue();
}

代码示例来源:origin: openhab/openhab1-addons

public Color toColor() {
  return Color.getHSBColor(hue.floatValue() / 360, saturation.floatValue() / 100, value.floatValue() / 100);
}

代码示例来源:origin: robolectric/robolectric

private static float round(float d, int decimalPlace) {
  BigDecimal bd = new BigDecimal(d);
  bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP);
  return bd.floatValue();
 }
}

代码示例来源:origin: stackoverflow.com

/**
  * Round to certain number of decimals
  * 
  * @param d
  * @param decimalPlace
  * @return
  */
 public static float round(float d, int decimalPlace) {
   BigDecimal bd = new BigDecimal(Float.toString(d));
   bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP);
   return bd.floatValue();
 }

代码示例来源:origin: ladingwu/dimens_sw

public static float px2dip(float pxValue, int sw,int designWidth) {
  float dpValue =   (pxValue/(float)designWidth) * sw;
  BigDecimal bigDecimal = new BigDecimal(dpValue);
  float finDp = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
  return finDp;
}

代码示例来源:origin: deeplearning4j/nd4j

/**
 * Return a float value representation.
 *
 * @return The value with single precision.
 */
public float floatValue() {
  BigDecimal adivb = (new BigDecimal(a)).divide(new BigDecimal(b), MathContext.DECIMAL128);
  return adivb.floatValue();
} /* Rational.floatValue */

代码示例来源:origin: prestodb/presto

@Override
  protected Number getNormalizedValue()
  {
    if (value.isNaN() || value.isInfinite()) {
      return value;
    }
    return new BigDecimal(getValue().floatValue()).round(new MathContext(precision)).floatValue();
  }
}

代码示例来源:origin: knowm/XChange

private static List<Float> getVolumeData(CondensedOrder[] condensedOrders) {

  List<Float> volumeData = new ArrayList<>();
  for (int i = 0; i < condensedOrders.length; i++) {
   volumeData.add(condensedOrders[i].getVolume().floatValue());
  }
  return volumeData;
 }
}

代码示例来源:origin: knowm/XChange

private static List<Float> getPriceData(CondensedOrder[] condensedOrders) {
 List<Float> priceData = new ArrayList<>();
 for (int i = 0; i < condensedOrders.length; i++) {
  priceData.add(condensedOrders[i].getPrice().floatValue());
 }
 return priceData;
}

代码示例来源:origin: knowm/XChange

private static List<Float> getPriceData(CondensedOrder[] condensedOrders) {
 List<Float> priceData = new ArrayList<>();
 for (int i = 0; i < condensedOrders.length; i++) {
  priceData.add(condensedOrders[i].getPrice().floatValue());
 }
 return priceData;
}

代码示例来源:origin: knowm/XChange

private static List<Float> getVolumeData(CondensedOrder[] condensedOrders) {
 List<Float> volumeData = new ArrayList<>();
 for (int i = 0; i < condensedOrders.length; i++) {
  volumeData.add(condensedOrders[i].getVolume().floatValue());
 }
 return volumeData;
}

代码示例来源:origin: apache/hive

public static float fastFloatValue(
  int fastSignum, long fast0, long fast1, long fast2,
  int fastIntegerDigitCount, int fastScale) {
 if (fastSignum == 0) {
  return 0;
 }
 BigDecimal bigDecimal = fastBigDecimalValue(
   fastSignum, fast0, fast1, fast2,
   fastIntegerDigitCount, fastScale);
 return bigDecimal.floatValue();
}

代码示例来源:origin: spotbugs/spotbugs

@SuppressFBWarnings("FE_FLOATING_POINT_EQUALITY")
  private void addApprox(BigDecimal roundFloor) {
    double approxDouble = roundFloor.doubleValue();
    if (approxDouble != value && Math.abs(approxDouble - value) / value < 0.001) {
      approxSet.add(approxDouble);
    }
    float approxFloat = roundFloor.floatValue();
    if (Math.abs(approxFloat - value) / value < 0.001) {
      approxSet.add(approxFloat);
      approxSet.add((double) approxFloat);
    }
  }
}

代码示例来源:origin: apache/hive

@HiveDecimalVersionV1
public float floatValue() {
 return bd.floatValue();
}

代码示例来源:origin: BroadleafCommerce/BroadleafCommerce

@Override
public void writeExternal(ObjectOutput out) throws IOException {
  // Write out the client properties from the server representation.
  out.writeFloat(amount.floatValue());
  // out.writeObject(currency);
}

代码示例来源:origin: json-path/JsonPath

public Float convert(Object src) {
    if(src == null){
      return null;
    }
    if(Float.class.isAssignableFrom(src.getClass())){
      return (Float) src;
    } else if (Integer.class.isAssignableFrom(src.getClass())) {
      return ((Integer) src).floatValue();
    } else if (Long.class.isAssignableFrom(src.getClass())) {
      return ((Long) src).floatValue();
    } else if (BigDecimal.class.isAssignableFrom(src.getClass())) {
      return ((BigDecimal) src).floatValue();
    } else if (Double.class.isAssignableFrom(src.getClass())) {
      return ((Double) src).floatValue();
    } else if (String.class.isAssignableFrom(src.getClass())) {
      return Float.valueOf(src.toString());
    }
    throw new MappingException("can not map a " + src.getClass() + " to " + Float.class.getName());
  }
}

代码示例来源:origin: bwssytems/ha-bridge

private static Integer calculateMath(Map<String, BigDecimal> variables, String mathDescriptor) {
    Integer endResult = null;
    try {
      Expression exp = new Expression(mathDescriptor);
      BigDecimal result = exp.eval(variables);
      endResult = Math.round(result.floatValue());
    } catch (Exception e) {
      log.warn("Could not execute Math: " + mathDescriptor, e);
      endResult = null;
    }
    return endResult;
  }
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testCustomNumberEditorWithFrenchBigDecimal() throws Exception {
  NumberFormat nf = NumberFormat.getNumberInstance(Locale.FRENCH);
  NumberTestBean tb = new NumberTestBean();
  BeanWrapper bw = new BeanWrapperImpl(tb);
  bw.registerCustomEditor(BigDecimal.class, new CustomNumberEditor(BigDecimal.class, nf, true));
  bw.setPropertyValue("bigDecimal", "1000");
  assertEquals(1000.0f, tb.getBigDecimal().floatValue(), 0f);
  bw.setPropertyValue("bigDecimal", "1000,5");
  assertEquals(1000.5f, tb.getBigDecimal().floatValue(), 0f);
  bw.setPropertyValue("bigDecimal", "1 000,5");
  assertEquals(1000.5f, tb.getBigDecimal().floatValue(), 0f);
}

相关文章

微信公众号

最新文章

更多