Answered by:
convert to double is not working

Question
-
User1052662409 posted
Hi
I am converting a long type to double for satisfying a condition, but it is not working.
foreach (S3Object entry in response.S3Objects) { if (Stringcl.GetValue(entry.Key).ToLower().Contains("signed release") && Stringcl.GetValue(entry.Key).ToLower().Contains(".pdf")) { if (Convert.ToDouble(entry.Size / 1024 / 1024) > 3.5) { oUtilcl.LogMessage("File size is greater than 3.5 MB"); return "File size is greater than 3.5 MB"; } else { // always goes in this block }
I am converting the entry.Size into MB (as it is in bytes) if (Convert.ToDouble(entry.Size / 1024 / 1024) > 3.5) and the value of [entry.Size / 1024 / 1024] = 3.62 but still it goes into else block.
Why?
Thursday, January 11, 2018 12:23 PM
Answers
-
User475983607 posted
I am converting the entry.Size into MB (as it is in bytes) if (Convert.ToDouble(entry.Size / 1024 / 1024) > 3.5) and the value of [entry.Size / 1024 / 1024] = 3.62 but still it goes into else block.
Why?
Simply, the division is done before the Convert.ToDouble method is invoked and the math produces an integer. Try this...
if (Convert.ToDouble((double)entry.Size / 1024d / 1024d) > 3.5d)
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, January 11, 2018 12:33 PM -
User753101303 posted
Hi,
You are working with integers so you produce an integer that you then convert to a double. You should convert the size and then do the calculation. For example try a console app such as :
static void Main(string[] args) { int size = 3795845; Console.WriteLine(Convert.ToDouble(size / 1024 / 1024)); // 3 Console.WriteLine(Convert.ToDouble(size) / 1024 / 1024); // around 3.62 }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, January 11, 2018 12:34 PM
All replies
-
User475983607 posted
I am converting the entry.Size into MB (as it is in bytes) if (Convert.ToDouble(entry.Size / 1024 / 1024) > 3.5) and the value of [entry.Size / 1024 / 1024] = 3.62 but still it goes into else block.
Why?
Simply, the division is done before the Convert.ToDouble method is invoked and the math produces an integer. Try this...
if (Convert.ToDouble((double)entry.Size / 1024d / 1024d) > 3.5d)
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, January 11, 2018 12:33 PM -
User753101303 posted
Hi,
You are working with integers so you produce an integer that you then convert to a double. You should convert the size and then do the calculation. For example try a console app such as :
static void Main(string[] args) { int size = 3795845; Console.WriteLine(Convert.ToDouble(size / 1024 / 1024)); // 3 Console.WriteLine(Convert.ToDouble(size) / 1024 / 1024); // around 3.62 }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, January 11, 2018 12:34 PM