skip to Main Content

How to encode Python Decimal to Json Number

How to convert Decimal("123456789012345.99") to json number 123456789012345.99? Python 3.11 win64 import json from decimal import Decimal class DecimalToFloat(json.JSONEncoder): def default(self, obj): if isinstance(obj, Decimal): return float(obj) return json.JSONEncoder.default(self, obj) class DecimalToStr(json.JSONEncoder): def default(self, obj): if isinstance(obj, Decimal): return str(obj)…

VIEW QUESTION

JSON parsing removes decimal trailing zeros

I have a Json string, after parsing I want it to remain in decimal form. import play.api.libs.json._ val jsonString = """{"value":2.0}""" println(jsonString) println(Json.parse(jsonString)) The output of the code above is: {"value":2.0} {"value":2} I need it to be {"value":2.0} {"value":2.0}

VIEW QUESTION

Mysql convert FLOAT to DECIMAL

I have mysql DB with important financial data, currently the data is stored as float type and I get incorrect data due to float rounding, I want to store it as DECIMAL. What is the safe way to convert the…

VIEW QUESTION

Swift show price depends on Locale – Ios swift

I am getting a string value that represents a price. I want to convert it to another string with specific precision and format it according to a current device Locale. Decimal(string: "123,4567", locale: NSLocale.current)?.formatted(.number.precision(.fractionLength(precision))) This code works for the german…

VIEW QUESTION
Back To Top
Search