Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conversion of long to decimal in c#

I have a value stored in a variable which is of type "long".

long fileSizeInBytes = FileUploadControl.FileContent.Length;
Decimal fileSizeInMB = Convert.ToDecimal(fileSizeInBytes / (1024 * 1024));

I want to convert the fileSizeInBytes to decimal number rounded up to 2 decimal places (like these: 1.74, 2.45, 3.51) But i'm not able to get the required result. I'm only getting single digit with no decimal places as result. Can some one help me with that ??.

Thanks in anticipation

like image 619
Sree Avatar asked May 01 '11 19:05

Sree


People also ask

How do you convert decimals to long?

Use the Convert. ToInt64() method to convert Decimal to Int64 (long) in C#.

Can long hold decimal values in C#?

long is just a big integer, it has nothing past the decimal point.

What is decimal number in C?

This information refers to fixed-point decimal data types as decimal data types. The decimal data type is an extension of the ANSI C language definition. When using the decimal data types, you must include the decimal. h header file in your source code.


1 Answers

Decimal fileSizeInMB = Convert.ToDecimal(fileSize) / (1024.0m * 1024.0m);

What you're doing is dividing the filesize by an integer, which results in an integer, not a decimal. Any remainder left over will be chopped off.

like image 145
robbrit Avatar answered Sep 20 '22 00:09

robbrit