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
Use the Convert. ToInt64() method to convert Decimal to Int64 (long) in C#.
long is just a big integer, it has nothing past the decimal point.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With