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 of numbers in c

The error is that %d in the format specifier of both scanf and printf represents decimal, so it expects an int (in the case of scanf, a pointer to int) Since you are declaring b and c as float, %d in the lines scanf(“%d”,&b); printf(“%d lbs is %d kgs.”,b,c); should be changed to %f respectively. … Read more

Allow only decimal numbers in textbox in C#

Although NumericUpDown is a good choice, but you want a textbox after all, right? Here is a general guide. First you need to subscribe to the TextChanged event of the textbox. This can be done by double clicking the textbox in the designer. When the event happens, you want to check the textbox’s text. If … Read more

Heavy numbers calculation in Java

A function to calculate whether or not a number is heavy might look something like: public boolean isHeavy(int number) { String asString = “” + number; int sumOfDigits = 0; for(int i = 0; i <asString.length(); i++) { sumOfDigits += Integer.parseInt(asString.charAt(i) + “”); } return (sumOfDigits / (double)asString.length()) > 7d; }