I saw double max size is : -1.79769313486232e308 to 1.79769313486232e308
But when i tried setting :
double bla = 99999999999999999999;
It says the number is too big.
Is double really that big? (e308).
You need to add d to the end of the number to specify that it's a double.
double bla = 99999999999999999999d;
Without the d suffix, it's actually a constant integer (or would be, if it wasn't out of integer range) which is then cast to double.
Also note that because of floating point precision, the number you get back is actually 1E20 or 100000000000000000000.
If you actually want to work with integers in that range, you should look at using BigInteger instead.
At least one thing - 99999999999999999999 is not a double constant. Make it
99999999999999999999.0
and it should work.
http://www.blackwasp.co.uk/CSharpNumericLiterals.aspx
has more insformation on all numeric literals.
Related
I have number of type double.
double a = 12.00
I have to make it as 12 by removing .00
Please help me
Well 12 and 12.00 have exactly the same representation as double values. Are you trying to end up with a double or something else? (For example, you could cast to int, if you were convinced the value would be in the right range, and if the truncation effect is what you want.)
You might want to look at these methods too:
Math.Floor
Math.Ceiling
Math.Round (with variations for how to handle midpoints)
Math.Truncate
If you just need the integer part of the double then use explicit cast to int.
int number = (int) a;
You may use Convert.ToInt32 Method (Double), but this will round the number to the nearest integer.
value, rounded to the nearest 32-bit signed integer. If value is
halfway between two whole numbers, the even number is returned; that
is, 4.5 is converted to 4, and 5.5 is converted to 6.
Use Decimal.Truncate
It removes the fractional part from the decimal.
int i = (int)Decimal.Truncate(12.66m)
Use Math.Round
int d = (int) Math.Round(a, 0);
Reading all the comments by you, I think you are just trying to display it in a certain format rather than changing the value / casting it to int.
I think the easiest way to display 12.00 as "12" would be using string format specifiers.
double val = 12.00;
string displayed_value = val.ToString("N0"); // Output will be "12"
The best part about this solution is, that it will change 1200.00 to "1,200" (add a comma to it) which is very useful to display amount/money/price of something.
More information can be found here:
https://msdn.microsoft.com/en-us/library/kfsatb94(v=vs.110).aspx
here is a trick
a = double.Parse(a.ToString().Split(',')[0])
Because the numbers after point is only zero, the best solution is to use the Math.Round(MyNumber)
//I am doing a basic Calculation here and this works for me.
// it takes values from textboxes and performs the following as far as I understand it. as I have only been coding now for a few months.
double VC2 = Convert.ToDouble(txt_VC_M16_Tap.Text); // converts to double.
double total2 = (VC2 * 1000) / (3.14157 * 14.5); // performs the calculation.
total2 = Math.Round(total2); //Round the result to a whole number(integer)
txt_RPM2.Text = Convert.ToString(total2); // converts result to string and puts it in the textbox as required.
// Hope this helps people that are looking for simple answers.
I want to be able to convert a
varchar > decimal > long
when dbreader[11] holds a 3319419,13 as varchar
For example:
decimal d = Convert.ToDecimal(dbreader[11], CultureInfo.InvariantCulture);
I get
d = 3319419.13M
When i convert this to long object I end up with
long i = 3319419
I want to end up with 3319419,1
It never will, I'm afraid. long is a synonym for System.Int64 - a 64 bit integer value so it is unable to store the precision you are asking it to. See here
You cannot end up with 3319419,1 in a long, because long is integer.
Try using a double instead.
EDIT: Now, double happens to be a floating-point number. This means that the number of decimal digits that this type is capable of representing is not fixed, but variable. So, if you want to see a specific number of decimal digits, then this is entirely a matter of display. So, in order to see your double variable with only one digit, convert it to string as follows: String.Format( "{0:00.0}", my_double_variable );
A long is an 8 byte integer (whole number). If you require more precision, use a double (8 byte decimal)
Reality check.
3319419,1
A long is incapable of keeping partial numbers. 1, 2, 3 - not 1, 1.1, 1.2. This is teh definition of a long.
SO, you CAN NOT keep fractional values in a long, regardless what you want. Want to keep fractional values - use a data type that can.
A long does not have any decimals. It is an integer type.
You might want to use double, or keep it as a decimal.
It is ok to do this?
double doubleVariable=0.0;
if (doubleVariable==0) {
...
}
Or this code would suffer from potential rounding problems?
Nope it's perfectly legal if you are only going to compare against 0 as the right side of comparison will automatically casted to double. On the other hand, it would yield all the round-off errors if you where to compare against == 0.10000001
You are better or reading the discussion about float to 0 comparison here: Is it safe to check floating point values for equality to 0?
Also this discussion is very informative about weird precision problems on floats: Why the result is different for this problem?
i.e. below will yield false:
double d1 = 1.000001; double d2 =0.000001;
Console.WriteLine((d1-d2)==1.0);
What you have there is 0, which is an integer literal. It is implicitly converted to a double which you could represent with the double literal 0.0 (implicit conversion). Then there is a comparison between the two doubles. A rounding error could cause doubleVariable to not be equal to 0.0 (by some other math you might do, not just setting it), but there could never be a rounding error when converting the integer 0 to double. The code you have there is totally safe, but I would favor == 0.0 instead.
Try:
if (double.Equals(doubleValue, 0.0)){}
If you're just comparing a double variable against 0.0 (or 0), I believe it's safe to do it that way because I think 0 can be represented exactly in floating point, but I'm not 100% sure.
In general, the suggested approach for comparing floating point numbers is to choose a "delta" value at which you'll consider two doubles to be equal if their difference is less than the delta. This handles exact representation limitations with floating point numbers.
double first = 1.234;
double second = 1.2345;
double difference = Math.Abs(first - second);
double threshold = 0.000001; // doubles are equal if their difference is less than this value - you choose this value based on your needs
bool areEqual = difference < threshold;
hmm... I think as long as the number has an exact binary fraction representation (like 0) the comparison is perfectly valid.
You should not use double for such comparision.
double creates problem.
e.g double n1=0.55 double n2=100 then double ans=n1*n2 should be 55.0
but when you debug ans is 55.000000000000007.
if(ans==55.0)
will fail. in such case you can face a problem.
This i what I am trying to achieve:
If a double has more than 3 decimal places, I want to truncate any decimal places beyond the third. (do not round.)
Eg.: 12.878999 -> 12.878
If a double has less than 3 decimals, leave unchanged
Eg.: 125 -> 125
89.24 -> 89.24
I came across this command:
double example = 12.34567;
double output = Math.Round(example, 3);
But I do not want to round. According to the command posted above,
12.34567 -> 12.346
I want to truncate the value so that it becomes: 12.345
Doubles don't have decimal places - they're not based on decimal digits to start with. You could get "the closest double to the current value when truncated to three decimal digits", but it still wouldn't be exactly the same. You'd be better off using decimal.
Having said that, if it's only the way that rounding happens that's a problem, you can use Math.Truncate(value * 1000) / 1000; which may do what you want. (You don't want rounding at all, by the sounds of it.) It's still potentially "dodgy" though, as the result still won't really just have three decimal places. If you did the same thing with a decimal value, however, it would work:
decimal m = 12.878999m;
m = Math.Truncate(m * 1000m) / 1000m;
Console.WriteLine(m); // 12.878
EDIT: As LBushkin pointed out, you should be clear between truncating for display purposes (which can usually be done in a format specifier) and truncating for further calculations (in which case the above should work).
I can't think of a reason to explicitly lose precision outside of display purposes. In that case, simply use string formatting.
double example = 12.34567;
Console.Out.WriteLine(example.ToString("#.000"));
double example = 3.1416789645;
double output = Convert.ToDouble(example.ToString("N3"));
Multiply by 1000 then use Truncate then divide by 1000.
If your purpose in truncating the digits is for display reasons, then you just just use an appropriate formatting when you convert the double to a string.
Methods like String.Format() and Console.WriteLine() (and others) allow you to limit the number of digits of precision a value is formatted with.
Attempting to "truncate" floating point numbers is ill advised - floating point numbers don't have a precise decimal representation in many cases. Applying an approach like scaling the number up, truncating it, and then scaling it down could easily change the value to something quite different from what you'd expected for the "truncated" value.
If you need precise decimal representations of a number you should be using decimal rather than double or float.
You can use:
double example = 12.34567;
double output = ( (double) ( (int) (example * 1000.0) ) ) / 1000.0 ;
Good answers above- if you're looking for something reusable here is the code. Note that you might want to check the decimal places value, and this may overflow.
public static decimal TruncateToDecimalPlace(this decimal numberToTruncate, int decimalPlaces)
{
decimal power = (decimal)(Math.Pow(10.0, (double)decimalPlaces));
return Math.Truncate((power * numberToTruncate)) / power;
}
In C lang:
double truncKeepDecimalPlaces(double value, int numDecimals)
{
int x = pow(10, numDecimals);
return (double)trunc(value * x) / x;
}
I'm using the following piece of code and under some mysterious circumstances the result of the addition is not as it's supposed to be:
double _west = 9.482935905456543;
double _off = 0.00000093248155508263153;
double _lon = _west + _off;
// check for the expected result
Debug.Assert(_lon == 9.4829368379380981);
// sometimes i get 9.48293685913086 for _lon (which is wrong)
I'm using some native DLLs within my application and i suspect that some DLL is responsible for this 'miscalculation', but i need to figure out which one.
Can anyone give me a hint how to figure out the root of my problem?
double is not completely accurate, try using decimal instead
The advanteage of using double and float over decimal is performance
At first I thought this was a rounding error but actually it is your assertion that is wrong. Try adding the entire result of your calculation without any arbitrary rounding on your part.
Try this:
using System;
class Program
{
static void Main()
{
double _west = 9.482935905456543;
double _off = 0.00000093248155508263153;
double _lon = _west + _off;
// check for the expected result
Console.WriteLine(_lon == 9.48293683793809808263153);
}
}
In the future though it is best to use System.Decimal in cases where you need to avoid rounding errors that are usually associated with the System.Single and System.Double types.
That being said, however, this is not the case here. By arbitrarily rounding the number at a given point you are assuming that the type will also round at that same point which is not how it works. Floating point numbers are stored to their maximum representational capacity and only once that threshold has been reached does rounding take place.
The problem is that Double only has precision of 15 - 16 digits (and you seem to need more precision in your example) whereas Decimal has precision to 28 - 29. How are you converting between Double and Decimal?
You are being bitten by rounding and precision problems. See this. Decimal might help. Go here for details on conversions and rounding.
From MSDN:
When you convert float or double to decimal, the source value is converted to decimal representation and rounded to the nearest number after the 28th decimal place if required. Depending on the value of the source value, one of the following results may occur:
If the source value is too small to be represented as a decimal, the result becomes zero.
If the source value is NaN (not a number), infinity, or too large to be represented as a decimal, an OverflowException is thrown.
You cannot represent every floating point number in the decimal system with a floating point in a binary system accurately, this isn't even directly related to how "small" the decimal number is, some numbers just don't "fit" in base-2 nicely.
Using a longer bit width can help in most cases, but not always.
To specify your constants in Decimal (128-bit floating point ) precision, use this declaration:
decimal _west = 9.482935905456543m;
decimal _off = 0.00000093248155508263153m;
decimal _lon = _west + _off;