Float vs Decimal in ActiveRecord

I remember my CompSci professor saying never to use floats for currency. The reason for that is how the IEEE specification defines floats in binary format. Basically, it stores sign, fraction and exponent to represent a Float. It’s like a scientific notation for binary (something like +1.43*10^2). Because of that, it is impossible to store … Read more

How can I remove the decimal part from JavaScript number?

You could use… Math.trunc() (truncate fractional part, also see below) Math.floor() (round down) Math.ceil() (round up) Math.round() (round to nearest integer) …dependent on how you wanted to remove the decimal. Math.trunc() isn’t supported on all platforms yet (namely IE), but you could easily use a polyfill in the meantime. Another method of truncating the fractional … Read more

Python JSON serialize a Decimal object

Simplejson 2.1 and higher has native support for Decimal type: >>> json.dumps(Decimal(‘3.9’), use_decimal=True) ‘3.9’ Note that use_decimal is True by default: def dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, encoding=’utf-8′, default=None, use_decimal=True, namedtuple_as_object=True, tuple_as_array=True, bigint_as_string=False, sort_keys=False, item_sort_key=None, for_json=False, ignore_nan=False, **kw): So: >>> json.dumps(Decimal(‘3.9’)) ‘3.9’ Hopefully, this feature will be included in standard library.

decimal vs double! – Which one should I use and when? [duplicate]

For money, always decimal. It’s why it was created. If numbers must add up correctly or balance, use decimal. This includes any financial storage or calculations, scores, or other numbers that people might do by hand. If the exact value of numbers is not important, use double for speed. This includes graphics, physics or other … Read more

How to round a number to n decimal places in Java

Use setRoundingMode, set the RoundingMode explicitly to handle your issue with the half-even round, then use the format pattern for your required output. Example: DecimalFormat df = new DecimalFormat(“#.####”); df.setRoundingMode(RoundingMode.CEILING); for (Number n : Arrays.asList(12, 123.12345, 0.23, 0.1, 2341234.212431324)) { Double d = n.doubleValue(); System.out.println(df.format(d)); } gives the output: 12 123.1235 0.23 0.1 2341234.2125 EDIT: … Read more

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