Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I want to get your views on the following line of code,, that what is this and what it does.

Actually i want to know that what will be returning in amount and chkAmount ?

C#
string amount =  Convert.ToDouble(56);


C#
string chkAmount = String.Format("{0:f}", amount)


Your kind views will help me to clear these basic things. :)
Posted
Comments
Sergey Alexandrovich Kryukov 12-Nov-13 11:09am    
There is one big fallacy of many beginners these days: working with strings representing data where they could work with data itself. What makes you having "amount" stored in string in first place, not as appropriate numeric type?
—SA

First statement is incorrect. It is clear that 56 is being converted to Double so it should be
Double amount =  Convert.ToDouble(56);

And for the second one see this : Standard Numeric Format Strings[^]
 
Share this answer
 
v2
Comments
VICK 12-Nov-13 5:08am    
Thanks... :)
Thanks7872 12-Nov-13 5:09am    
Cheers..!
Sergey Alexandrovich Kryukov 12-Nov-13 11:10am    
Sure, a 5. It's also worth noting: it always better to use Parse/TryParse whenever possible. At least, the name of method would reflect what really happens: this is parsing, not "conversion".
—SA
Thanks7872 12-Nov-13 11:41am    
Yes :thumbsup:

Thanks.
First line will throw compilation error, as you are trying to store a double value in string.

string amount =  Convert.ToDouble(56);


this line should be :
double amount = Convert.ToDouble(56);


Result : 56.0

and second line :

string chkAmount = String.Format("{0:f}", amount)


It will return : 56.00


For detail knowledge of String.Format please refer :
String Format for Double [C#][^]

Good luck.
 
Share this answer
 
Comments
VICK 12-Nov-13 5:10am    
Thanks.. but can u elaborate "{0:f}" a bit more in detail??
Sergey Alexandrovich Kryukov 12-Nov-13 11:11am    
How about reading MSDN by yourself?
—SA
Raje_ 12-Nov-13 9:56am    
Check out this link : http://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx
you will get all your answers.
Google is your friend, visit sometimes.
Sergey Alexandrovich Kryukov 12-Nov-13 11:11am    
5ed, but see also my comment to Solution 1.
—SA
Raje_ 13-Nov-13 4:57am    
Thank You.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900