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
Related
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.
I have a double in C# like below:
var myDouble = 1.0;
When I want to display this double, it prints as 1 when it should print as 1.0, I'm using this for a version so it kind of defeats the object.
How can I display it as its true value? 1.0 not 1...
Use
var myDouble = 1.0;
Console.WriteLine(myDouble.ToString("0.0"));
This question does not technically have an answer. The double is a 64-bit (and float as 32-bit) numeric floating point type. There's fundamentally no distinction between 1, 1.0, 1.00 and 1.000. These are all the SAME value.
In fact, that binary value if its 64-bit comes out to 3FF0000000000000 for ALL those examples. If its 32-bit, the binary expression is different but the idea is the same.
The format is defined by IEE 754 specification. More information on that is here:
https://en.wikipedia.org/wiki/IEEE_754
There are 3 sections if a floating point number:
1. Sign Bit
2. Exponent
3. Mantissa
There's no section for "number of zeros I typed".
Basically though, once you convert it to a number, you have lost the definition of how you had formatted the text, that just doesn't exist for a number.
If you want to remember how many decimal places you typed, you need to store it as a string. If you just want to format the decimal data type, you need to specify a format as there's no knowledge of your original format.
It is just as correct to say that "1" is its true value.
You can format the number as you want using something like this:
myDouble.ToString("0.0");
But I really don't understand why you would. A version is generally formatted a specific way. Some people format it as "1.00". I really think it makes more sense to store it in a string so it is always guaranteed to appear exactly as it should.
You should declare the variable in double type like this:
double a = 1.0;
Console.WriteLine(string.Format("{0:0.0}",a));
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)));
We have encountered an issue which is the double value is rounded after using .toString() method, in order to show in TextBox.
What we need is something like this:
Double value: 39621443.8975101998
String Value: 39621443.8975101998
But what we get is:
Double value: 39621443.8975101998
String Value: 39621443.8975102
We Have Googled it and tried many methodes, but none of then have worked
Any help would be highly appreciated
If you want exact value with that many decimals you should use the Decimal data type which has higher precision then double. Use decimal.Parse(str).
As #PetSerAl points out, the two values (as represented as an IEEE 754 double precision floating point number) are the same.
39621443.8975101998 => 0x4182E49A1F2E19D4
39621443.8975102 => 0x4182E49A1F2E19D4
39621443.897510199800000123456789 => 0x4182E49A1F2E19D4
Source: BinaryConvert.net.
RoundTrip won't help you here - this is the limit of double precision.
You will need another data type, like #magnus suggests.
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();