Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 不间断JSON格式十进制_Python_Json_Flask Restless - Fatal编程技术网

Python 不间断JSON格式十进制

Python 不间断JSON格式十进制,python,json,flask-restless,Python,Json,Flask Restless,我使用Flask Untivent 0.17作为API。API有几个小数字段。我可以通过安装simplejson成功返回小数,如下所述: 但是,当我有一个十进制字段是0时,该值是用科学符号表示的,例如0E-8。我想返回0.00000000 我正在尝试类似的东西,但无法让它工作。0仍然以科学记数法返回 class DecimalJSONEncoder(json.JSONEncoder): # Try to format the decimal # EDIT: but am now

我使用Flask Untivent 0.17作为API。API有几个小数字段。我可以通过安装simplejson成功返回小数,如下所述:

但是,当我有一个十进制字段是0时,该值是用科学符号表示的,例如
0E-8
。我想返回
0.00000000

我正在尝试类似的东西,但无法让它工作。0仍然以科学记数法返回

class DecimalJSONEncoder(json.JSONEncoder):
    # Try to format the decimal
    # EDIT: but am now realizing that this doesn't work
    # It was serializing the decimal at all because I had 
    # simplejson installed.
    def default(self, o):
        if isinstance(o, decimal.Decimal):
            return format(str(o), '.8f')
        return super(DecimalJSONEncoder, self).default(o)


def init_app(app):
    # .....
    # Use the Flask-Restless postprocessor to format.
    def postprocesor(result, **kw):
        json.dumps(result, cls=DecimalJSONEncoder)
当小数为零时,如何强制小数为
0.00000000