question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is there a setting to change doubles to decimals?

See original GitHub issue

I am trying to generate a CSharp Client from a swagger URL online using the NSwagStudio.

Some of the definitions look like this:

"initialInvestment": {
     "type": "number",
    "format": "double",
    "description": "The minimum initial investment required to purchase the fund"
},

It returns money as doubles, but I know I should be using decimals for money. Is there a setting in here somewhere to generate my POCOs using decimals any time it sees a type number with format double?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:25 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
adamjones1commented, Jun 28, 2019

Given that numbers are already converted to a decimal string format when serialising them as JSON across the wire, it would be preferable to have decimal as the default real number type in clients (or at the very least this should be easily configurable without custom code).

That is, although the spec format name is “double”, in JSON or XML this necessarily means “decimal approximation of double”, so by converting on the client side back to double you’re only approximating an approximation. Having only one level of approximation, taking the decimal conversion, would be better. At the moment I’m using just decimals on the server side anyway (no approximation) and I’m having to add in a custom pre-processor in every NSwag client to avoid the imprecision.

1reaction
more-urgent-jestcommented, Aug 14, 2022

I am using stoplight studio to create the spec for a new API and if I edit the json to define a property as:

          "amount": {
            "type": "number+decimal"
          },

then NSwagStudio v13.16.1.0 generates:

        public object Amount { get; set; }

unless you mean:

          "amount": {
            "type": "number",
            "format": "decimal"
          },

then NSwagStudio v13.16.1.0 generates:

        public decimal Amount { get; set; }
Read more comments on GitHub >

github_iconTop Results From Across the Web

C# correct and efficient conversion of double to decimal
I really think your way is better. You can simply cast the double to decimal: double doubleValue = 12.23; decimal decimalValue = (decimal) ......
Read more >
Java double decimal precision
How to set double precision to 2 decimal places? A double in Java is a 64-bit number, and the 64-bit precision of a...
Read more >
Why not allow double/decimal implicit conversions?
The easy answer is: there cannot be an implicit conversion from double to decimal because of the range discrepancy; a huge number of...
Read more >
Convert.ToDecimal Method (System)
Converts the value of the specified double-precision floating-point number to an equivalent decimal number. ToDecimal(Decimal).
Read more >
When should you use Decimal instead of Double?
I'm currently working on a project that primarily deals with money calculations and money estimations. I started out using Double for the values ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found