public void ToSingle() { BigDecimal fl1 = BigDecimal.Parse("234563782344567"); Assert.IsTrue(fl1.ToSingle() == 234563782344567f, "the float representation of bigDecimal 234563782344567"); BigDecimal fl2 = new BigDecimal(2.345E37); Assert.IsTrue(fl2.ToSingle() == 2.345E37F, "the float representation of bigDecimal 2.345E37"); fl2 = new BigDecimal(-1.00E-44); Assert.IsTrue(fl2.ToSingle() == -1.00E-44F, "the float representation of bigDecimal -1.00E-44"); fl2 = new BigDecimal(-3E12); Assert.IsTrue(fl2.ToSingle() == -3E12F, "the float representation of bigDecimal -3E12"); fl2 = new BigDecimal(Double.MaxValue); Assert.IsTrue(fl2.ToSingle() == Single.PositiveInfinity, "A number can't be represented by float should return infinity"); fl2 = new BigDecimal(-Double.MaxValue); Assert.IsTrue(fl2.ToSingle() == Single.NegativeInfinity, "A number can't be represented by float should return infinity"); }
public float ToSingle() { BigDecimal adivb = (new BigDecimal(Numerator)).Divide(new BigDecimal(Denominator), MathContext.Decimal128); return(adivb.ToSingle()); }