How to get decimal value .00 from Web API - c#

I have a web API method that returns decimal value 52.00, when I debug the web API the value is coming correctly but when it returns the result in postman it is becoming only 52 but it should come as 52.00 and if the decimal is more than two then round it to .00
Here the result when I debug
Here the result in postman

if you propertied data type is string then you can use
String.Format("{0:0.00}", 52.00);
decimalVar.ToString("F"); 23.456 → 23.46
if properties is decimal it self with round of
Decimal.Round(52.167, 2)
52.167 → 52.17
This might help.

52, 52.0, 52.00, or 52.000 are the same value.
What you want seems to be format a decimal, float, double, or int to 2 decimal.
Like Leave only two decimal places after the dot.
You can check the Msdn reference guide on Numerical string format if you need more specific format. https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings
But the question is still ambigiuous: What is the expected result when the precision is more than 2 decimal ? Do you want to trim or to round the value?
I will not apply those format to the Json, as you may loose precision, but only format the display.

Related

I'm getting rounding that I don't want when formatting a number in c#

As I understand from Microsoft's documentation, if I have 340550.46 and I apply the format string #,##0.00 I should get the string "340,550.46". But instead, I'm getting "340.550,50". I've set breakpoints to ensure that I know what the number and format strings are going in.
To be more specific, I have a string "340550.46", and I convert it to a float, and then use ToString() to format it with "#,##0.00":
float.Parse("340550.46").ToString("#,##0.00)
What am I doing wrong here? Why is it rounding the number, and what can I do to prevent that?
The reason for the rounding is that a float is limited to 7 signficant digits.
See: https://en.wikipedia.org/wiki/IEEE_754
If you changed to a double, you would get that 8th extra digit. Also, if you are formatting to 2 decimal places since it may be currency, consider using Decimal.

Double.Parse precision/Rounding issue

I have one column in database with datatype
decimal(24,10)
Let suppose I have value like
string d1 = "123.6666666666";
Double.Parse(d1)
output in datacolumn : 123.6666666700
I have used Convert.ToDecimal(d1) which gives output 123.6666666666.
Expected output :123.7000000000
The output you got is because the precision of double and decimal values. What you actualy want is to round the number to 1 fractional digit, for that you have to use Math.Round, either with a double or decimal, according to your needs, like this:
Math.Round(Double.Parse(d1),1)
Or, if you need
Math.Round(Convert.ToDecimal(d1),1)
For more information about Math.Round, check the MSDN link:
https://msdn.microsoft.com/pt-br/library/75ks3aby(v=vs.110).aspx

Verify and print double value in 2 decimal places

I am reading a string value and try to confirm its value its currency value via this method
double value;
if (!double.TryParse(sumValue, out value) || Math.Round(value, 2) != value)
{
MessageBox.Show("Not a double value");
}
This works fine. The issue when I use this MessageBox.Show(Math.Round(value, 2)) it does not show the value in 2 decimal places. What changes can I do for that and also am I using the right method to verify?
How the value is output will depend on the actual value.
While Math.Round(value, 2) will round the value to two decimal places, if that rounded value is 1.00 or 1.50 (for example) it will be displayed as "1" or "1.5" as the trailing zeros are omitted by default.
If you want to display the value to 2 decimal places then there are a number of ways to do this. All require you to call either string.Format or explicitly call .ToString with a format parameter.
One is to use:
MessageBox.Show(String.Format("{0:0.00}", value));
The first "0" represents the number itself and the the "0.00" indicates to the formatting engine that this is a floating point number with two decimal places. You can use "#:##" instead.
Source
Another is:
MessageBox.Show(value.ToString("F"));
Which is the fixed point format specifier. Adding a number specifies the number of decimal places (2 is the default). Source
Given that you say of your code that "This works fine." then your verification step is correct. You are checking that the value is a number and that the value rounded to 2 decimal places is the value you want. There's nothing more you need to do.
You can try to use .ToString() method with custom format, like this:
value.ToString("#.##");
Just use the code
MessageBox.Show(Convert.ToString(Math.Round(value, 2)));

Double to String Format text format

i have the follwing lines of code
double formId=2013519115027601;
txtEditFormID.Text = formid.ToString();
it gives me output
2.0135191150276E+15
if i write
txtEditFormID.Text = formId.ToString("0.0", CultureInfo.InvariantCulture);
it gives me
2013519115027600.0
but i want the label text
2013519115027601
how to do it?
I don't have enough information about the usage of your formId variable.
As it is shown above it seems an error to use a double datatype when there is no decimals to work on. So redefining your variable as a long datatype will be easy and the conversion will be the same.
long formId=2013519115027601;
txtEditFormID.Text = formid.ToString();
Not to mention the added benefit to your code to work with whole numbers instead of floating point numbers.
However, if you want to maintain the current datatype then
txtEditFormID.Text = formId.ToString("R");
The Round Trip Format Specifier
When a Single or Double value is formatted using this specifier, it is
first tested using the general format, with 15 digits of precision for
a Double and 7 digits of precision for a Single. If the value is
successfully parsed back to the same numeric value, it is formatted
using the general format specifier. If the value is not successfully
parsed back to the same numeric value, it is formatted using 17 digits
of precision for a Double and 9 digits of precision for a Single.
Your first option is to use data type as long or decimal . Something else you can do if you want to keep using double is this :
double formId = 2013519115027601;
string text = formId.ToString();
txtEditFormID.Text = text.Replace(".",string.Empty);
this will remove all the '.' chars
There are times where I want calculations handled in double but I want the result displayed as as an int or even rounded amount, so the question isn't so strange (assuming that the given sample is simplified in order to ask the question).
I was going to post sample code for rounding, but it makes more sense to just use the built-in method Math.Round(). You can cast to a long, as mentioned above, but you won't have rounding, if desired (which it usually is, IMHO).
txtEditFormId.Text = ((long)formId).ToString();

C# Convert.ToDouble() loses decimal points when converting string to double

Let's say we have the following simple code
string number = "93389.429999999993";
double numberAsDouble = Convert.ToDouble(number);
Console.WriteLine(numberAsDouble);
after that conversion numberAsDouble variable has the value 93389.43. What can i do to make this variable keep the full number as is without rounding it? I have found that Convert.ToDecimal does not behave the same way but i need to have the value as double.
-------------------small update---------------------
putting a breakpoint in line 2 of the above code shows that the numberAsDouble variable has the rounded value 93389.43 before displayed in the console.
93389.429999999993 cannot be represented exactly as a 64-bit floating point number. A double can only hold 15 or 16 digits, while you have 17 digits. If you need that level of precision use a decimal instead.
(I know you say you need it as a double, but if you could explain why, there may be alternate solutions)
This is expected behavior.
A double can't represent every number exactly. This has nothing to do with the string conversion.
You can check it yourself:
Console.WriteLine(93389.429999999993);
This will print 93389.43.
The following also shows this:
Console.WriteLine(93389.429999999993 == 93389.43);
This prints True.
Keep in mind that there are two conversions going on here. First you're converting the string to a double, and then you're converting that double back into a string to display it.
You also need to consider that a double doesn't have infinite precision; depending on the string, some data may be lost due to the fact that a double doesn't have the capacity to store it.
When converting to a double it's not going to "round" any more than it has to. It will create the double that is closest to the number provided, given the capabilities of a double. When converting that double to a string it's much more likely that some information isn't kept.
See the following (in particular the first part of Michael Borgwardt's answer):
decimal vs double! - Which one should I use and when?
A double will not always keep the precision depending on the number you are trying to convert
If you need to be precise you will need to use decimal
This is a limit on the precision that a double can store. You can see this yourself by trying to convert 3389.429999999993 instead.
The double type has a finite precision of 64 bits, so a rounding error occurs when the real number is stored in the numberAsDouble variable.
A solution that would work for your example is to use the decimal type instead, which has 128 bit precision. However, the same problem arises with a smaller difference.
For arbitrary large numbers, the System.Numerics.BigInteger object from the .NET Framework 4.0 supports arbitrary precision for integers. However you will need a 3rd party library to use arbitrary large real numbers.
You could truncate the decimal places to the amount of digits you need, not exceeding double precision.
For instance, this will truncate to 5 decimal places, getting 93389.42999. Just replace 100000 for the needed value
string number = "93389.429999999993";
decimal numberAsDecimal = Convert.ToDecimal(number);
var numberAsDouble = ((double)((long)(numberAsDecimal * 100000.0m))) / 100000.0;

Categories

Resources