Best way to store currency values in C++

Don’t store it just as cents, since you’ll accumulate errors when multiplying for taxes and interest pretty quickly. At the very least, keep an extra two significant digits: $12.45 would be stored as 124,500. If you keep it in a signed 32 bit integer, you’ll have $200,000 to work with (positive or negative). If you … Read more

What is “The Best” U.S. Currency RegEx?

here’s some stuff from the makers of Regex Buddy. These came from the library so i’m confident they have been thoroughly tested. Number: Currency amount (cents mandatory) Optional thousands separators; mandatory two-digit fraction Match; JGsoft: ^[+-]?[0-9]{1,3}(?:,?[0-9]{3})*\.[0-9]{2}$ Number: Currency amount (cents optional) Optional thousands separators; optional two-digit fraction Match; JGsoft: ^[+-]?[0-9]{1,3}(?:,?[0-9]{3})*(?:\.[0-9]{2})?$ Number: Currency amount US & … Read more

Using BigDecimal to work with currencies

Here are a few hints: Use BigDecimal for computations if you need the precision that it offers (Money values often need this). Use the NumberFormat class for display. This class will take care of localization issues for amounts in different currencies. However, it will take in only primitives; therefore, if you can accept the small … Read more

Format currency without currency symbol

The following works. It’s a bit ugly, but it fulfils the contract: NumberFormat nf = NumberFormat.getCurrencyInstance(); DecimalFormatSymbols decimalFormatSymbols = ((DecimalFormat) nf).getDecimalFormatSymbols(); decimalFormatSymbols.setCurrencySymbol(“”); ((DecimalFormat) nf).setDecimalFormatSymbols(decimalFormatSymbols); System.out.println(nf.format(12345.124).trim()); You could also get the pattern from the currency format, remove the currency symbol, and reconstruct a new format from the new pattern: NumberFormat nf = NumberFormat.getCurrencyInstance(); String pattern = … Read more

Formatting money in twig templates

The number_format filter has been included in the Twig core since the end of December 2011. The relevant commit is here. Usage: number_format(decimals, decimalSeparator, thousandSeparator) {{ total|number_format(2) }} {{ total|number_format(0, ‘.’) }} {{ total|number_format(2, ‘.’, ‘,’) }} Read more about it in the docs

Use float or decimal for accounting application dollar amount?

Should Float or Decimal data type be used for dollar amounts? The answer is easy. Never floats. NEVER! Floats were according to IEEE 754 always binary, only the new standard IEEE 754R defined decimal formats. Many of the fractional binary parts can never equal the exact decimal representation. Any binary number can be written as … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)