Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prevent rounding of double datatype values #1580

Closed
cppdeveloper28 opened this issue Apr 24, 2019 · 2 comments
Closed

prevent rounding of double datatype values #1580

cppdeveloper28 opened this issue Apr 24, 2019 · 2 comments
Labels
kind: question solution: invalid the issue is not related to the library

Comments

@cppdeveloper28
Copy link

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.
}

@nlohmann
Copy link
Owner

This is not a library issue.

The dump function in the library creates as many digits are necessary to roundtrip to the closest double number.

@nlohmann nlohmann added the solution: invalid the issue is not related to the library label Apr 24, 2019
@stale
Copy link

stale bot commented May 24, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated label May 24, 2019
@nlohmann nlohmann removed the state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated label Jun 21, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: question solution: invalid the issue is not related to the library
Projects
None yet
Development

No branches or pull requests

2 participants