【问题标题】:format of the numbers in json-cjson-c中数字的格式
【发布时间】:2013-10-19 20:20:42
【问题描述】:

我想要添加到 json 对象的数字远低于零(例如 1.34e-14)。为此,我正在使用以下代码:

smallnumber=1.34e-14;
struct json_object *pointj=json_object_new_object();
json_object_object_add(pointj,"par", json_object_new_double(smallnumber);
cout << "\nThe json object created: " <<  json_object_to_json_string(pointj);

问题是数字被截断为 0.000000。是否可以指定输出的格式以使用科学计数法?

【问题讨论】:

    标签: c# json double truncated json-c


    【解决方案1】:

    这个问题可能有更优雅的解决方案,但我是这样解决的:

    double smallnumber=1.34e-14;
    stringstream tmp;
    tmp << smallnumber;
    struct json_object *pointj=json_object_new_object();
    json_object_object_add(pointj,"par", json_object_new_string(tmp.str().c_str());
    

    一切正常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-17
      • 2021-12-17
      • 1970-01-01
      • 2021-09-25
      • 1970-01-01
      • 2016-03-13
      • 1970-01-01
      相关资源
      最近更新 更多