decimal to long - always missing a dot separator - c#

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.

Related

Force decimal point and 0's after decimal with decimal.Parse

I'm converting a string to a decimal with decimal.Parse():
decimal.Parse(transactionAmount)
If transactionAmount contains a whole number such as 1, the result is a decimal value of 1. The system I'm sending it to outside of my program treats it as 1 cent for some unknown reason unless it shows up as 1.00. How can I make sure that whole numbers contain a decimal point and a zero such as 1.0?
decimal contains number of digits after the point as part of its internal presentation. So 1m and 1.00m are different `decimal values. As result all parsing/formatting operations will try to preserve that information coming from/to string form unless forced otherwise.
One hack to make sure there are at least two digits after decimal separator is to add proper 0 - 0.00m:
decimal decimalOne = decimal.Parse("1"); // 1.
decimal decimalWithTwoDigit = decimalOne + 0.00m; // 1.00
Note that it is unusual to be sending decimal values in binary form to outside programs. Most likely you actually need to format decimal value with two digits only as covered in Force two decimal places in C# - .ToString("#.00").
Try Convert.ToDecimal() instead of decimal.Parse()

Int.TryParse() returns false always

I have following code
int varOut;
int.TryParse(txt1.Text, out varOut); // Here txt1.Text = 4286656181793660
Here txt1.Text is the random 16 digit number generated by JavaScript which is an integer. But the above code always return false i.e. varOut value is always zero.
What I am doing wrong here ?
The limit for int (32-bit integer) is -2,147,483,648 to 2,147,483,647. Your number is too large.
For large integer number such as your case, try to Parse using long.TryParse (or Int64.TryParse since Int64 is long in C#) instead. The limit for long number is of the range of -9.2e18 to 9.2e18*
long varOut;
long.TryParse(txt1.Text, out varOut); // Here txt1.Text = 4286656181793660
It should be sufficient for your number, which is only around 4.2e15 (4,286,656,181,793,660).
Alternatively, you may want to consider using decimal.TryParse if you want to have decimal number (containing fraction, higher precision).
decimal varOut;
decimal.TryParse(txt1.Text, out varOut); // Here txt1.Text = 4286656181793660
It is 128-bit data type, with the range of -7.9e28 to 7.9e28, and 28-29 significant digits precision, fits best for any calculation involving money.
And, as a last remark to complete the answer, it may be unsafe to use double - do not use it. Although double has a very high range of ±5.0 × 10e−324 to ±1.7 × 10e308, its precision is only about 15-16 digits (reference).
double varOut;
double.TryParse(txt1.Text, out varOut); // Not a good idea... since the input number is 16-digit Here txt1.Text = 4286656181793660
In this case, your number consists of 16 digits, which is in the borderline of the double precision. Thus, in some cases, you may end up with wrong result. Only if you are sure that your number will be at most 15-digit precision that you are safe to use it.
*-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
int is just shorthand for int32; it's a 32 bit (signed) integer, meaning that it can't hold a number larger than around 2 billion. Your number is larger than that, and so is not a valid int value.
Use MaxLength property to limit the number of digits and user cannot enter more than int32.
TextBox.MaxLength=9
Looks like you may be using value(s) which exceed the capacity of the type you're using... look at https://msdn.microsoft.com/en-us/library/system.int32.maxvalue%28v=vs.110%29.aspx
Store it as a long instead of an int.
https://msdn.microsoft.com/en-us/library/ctetwysk.aspx
You should use long instead of int. Your number is too large for int
Use long.TryParse()
Your number is too large to convert into int.
or you can use int64.tryparse

Long to decimal with decimal point at a specific position

In C#, I have a long that I need to convert to a decimal. The problem is that I want to put the decimal point at a specific position in the long.
For example, let's say I have the following number as a long:
long l = 123456789
When converting to a decimal with the floating point at the third position, I want to have the following:
decimal d = 123456.789
To give you an example, the function would ideally be something like the BigDecimal.valueOf in Java that allows you to give a long and a position to put the decimal point, and it returns the correct value.
I know one of the solution would be to format the long as a string with the correct decimal point position and then convert it back to a decimal. Another solution would be to multiply the long by 10-Decimal places wanted, but I'm wondering if there's a better alternative to this.
Why don't you divide by 1000 (or 10^N where N is the number of decimal places)?
long l = 123456789L;
int decplaces = 3;
decimal divisor =(decimal) Math.Pow(10, decplaces);
decimal d = l/divisor;
You could use the Decimal constructor that takes different constituent parts. You'll need to do a bit of bitshifting to get the exponent into all the relevant sections, but that shouldn't be too hard. If you know that your value won't be more than 62 bits in magnitude, that would make things easier, too.
You could just write a loop to multiply by 0.1m the correct number of times, i.e. numberOfDecimalPlacesWanted iterations of the loop.

Double maxsize in .net

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.

How to remove decimal part from a number in C#

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.

Categories