Why aren’t Floating-Point Decimal numbers hardware accelerated like Floating-Point Binary numbers?

The latest revision of the IEEE 754:2008 standard does indeed define hardware decimal floating point numbers, using the representations shown in the software referenced in the question. The previous version of the standard (IEEE 754:1985) did not provide decimal floating point numbers. Most current hardware implements the 1985 standard and not the 2008 standard, but … Read more

Exact decimal datatype for C++?

The Boost.Multiprecision library has a decimal based floating point template class called cpp_dec_float, for which you can specify any precision you want. #include <iostream> #include <iomanip> #include <boost/multiprecision/cpp_dec_float.hpp> int main() { namespace mp = boost::multiprecision; // here I’m using a predefined type that stores 100 digits, // but you can create custom types very easily … Read more

C/C++ counting the number of decimals?

Two ways I know of, neither very clever unfortunately but this is more a limitation of the environment rather than me 🙂 The first is to sprintf the number to a big buffer with a “%.50f” format string, strip off the trailing zeros then count the characters after the decimal point. This will be limited … Read more

NSNumberFormatter with comma decimal separator

I would recommend not hardcoding the separator to ensure the right separator behavior based on the iPhone locale setting. The easiest way to to this is: using objective-c NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc]init]; numberFormatter.locale = [NSLocale currentLocale];// this ensures the right separator behavior numberFormatter.numberStyle = NSNumberFormatterDecimalStyle; numberFormatter.usesGroupingSeparator = YES; // example for writing the number … Read more

CAST to DECIMAL in MySQL

From MySQL docs: Fixed-Point Types (Exact Value) – DECIMAL, NUMERIC: In standard SQL, the syntax DECIMAL(M) is equivalent to DECIMAL(M,0) So, you are converting to a number with 2 integer digits and 0 decimal digits. Try this instead: CAST((COUNT(*) * 1.5) AS DECIMAL(12,2))

How can I format decimal property to currency?

Properties can return anything they want to, but it’s going to need to return the correct type. private decimal _amount; public string FormattedAmount { get { return string.Format(“{0:C}”, _amount); } } Question was asked… what if it was a nullable decimal. private decimal? _amount; public string FormattedAmount { get { return _amount == null ? … Read more