#ifndef DecimalNumber_h
#define DecimalNumber_h
#include <math.h>
#include <wtf/dtoa.h>
#include <wtf/MathExtras.h>
#include <wtf/text/WTFString.h>
namespace WTF {
enum RoundingSignificantFiguresType { RoundingSignificantFigures };
enum RoundingDecimalPlacesType { RoundingDecimalPlaces };
class DecimalNumber {
public:
DecimalNumber(double d)
{
ASSERT(!isnan(d) && !isinf(d));
dtoa(m_significand, d, m_sign, m_exponent, m_precision);
ASSERT(m_precision);
ASSERT(m_significand[0] != '0' || !m_exponent);
ASSERT(m_significand[0] != '0' || m_precision == 1);
ASSERT(m_significand[0] == '0' || m_significand[m_precision - 1] != '0');
}
DecimalNumber(double d, RoundingSignificantFiguresType, unsigned significantFigures)
{
ASSERT(!isnan(d) && !isinf(d));
dtoaRoundSF(m_significand, d, significantFigures, m_sign, m_exponent, m_precision);
ASSERT(significantFigures && significantFigures <= sizeof(DtoaBuffer));
while (m_precision < significantFigures)
m_significand[m_precision++] = '0';
ASSERT(m_precision);
ASSERT(m_significand[0] != '0' || !m_exponent);
}
DecimalNumber(double d, RoundingDecimalPlacesType, unsigned decimalPlaces)
{
ASSERT(!isnan(d) && !isinf(d));
dtoaRoundDP(m_significand, d, decimalPlaces, m_sign, m_exponent, m_precision);
unsigned significantFigures = 1 + m_exponent + decimalPlaces;
ASSERT(significantFigures && significantFigures <= sizeof(DtoaBuffer));
while (m_precision < significantFigures)
m_significand[m_precision++] = '0';
ASSERT(m_precision);
ASSERT(m_significand[0] != '0' || !m_exponent);
}
unsigned bufferLengthForStringDecimal() const;
unsigned bufferLengthForStringExponential() const;
unsigned toStringDecimal(UChar* buffer, unsigned bufferLength) const;
unsigned toStringExponential(UChar* buffer, unsigned bufferLength) const;
bool sign() const { return m_sign; }
int exponent() const { return m_exponent; }
const char* significand() const { return m_significand; } unsigned precision() const { return m_precision; }
private:
bool m_sign;
int m_exponent;
DtoaBuffer m_significand;
unsigned m_precision;
};
}
using WTF::DecimalNumber;
using WTF::RoundingSignificantFigures;
using WTF::RoundingDecimalPlaces;
#endif // DecimalNumber_h