Skip to content

prevent rounding of double datatype values #1580

@cppdeveloper28

Description

@cppdeveloper28

i have a requirement where i don't want json library to round the values of double datatype. Is there we can prevent it ?
e.g.
when i call "json.dump" function on my double variable "Amount" it gets rounded values instead of actual values.

  1. The Actual value Amount = 2555.5599999999999 gets rounded to 2555.56
  2. The Actual value Amount = 2527.7800000000002 gets rounded to 2527.78

is there any way we can stop rounding ?

code looks like
struct PaymentInfo
{
double Amount;
std:string Currency ;
};

I have implemented "to_json" function for the above structure.

in the main function , i do following.
main()
{
PaymentInfo Pay_obj ;
Pay_obj.Amount = 2555.5599999999999;
Pay_obj.Currency = "USD";

json json_obj = Pay_obj;
std:: stringstream output;
output << json_obj.dump(); // after this statement the value of Amount gets rounded to 2555.56.
// i want to retain the original value as it is. is there a way we can to
// it.
}

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions