Click here to Skip to main content
15,901,666 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
MyCode:

txtNdb.Text = dtCustomPrice.Rows(0)("curNDB").ToString()

values from db
dtCustomPrice.Rows(0)("curNDB").ToString() is 300.0000

i need value in txtNdb.Text as 300.00,currently i'm getting as 300.0000

How to format
Posted
Comments
SathishRam 12-Oct-15 5:13am    
[String].Format,ToString("#.##") i have already tried all these

Hi,

Check this...

Custom Numeric Format String[^]

C#
value = 1234.567890;
Console.WriteLine(value.ToString("0,0.00", CultureInfo.InvariantCulture));
Console.WriteLine(String.Format(CultureInfo.InvariantCulture,
                                "{0:0,0.00}", value));

// Displays 1,234.57 





Hope this will help you.

Cheers
 
Share this answer
 
v2
Comments
SathishRam 12-Oct-15 5:28am    
sorry Magic Wonder,not working
this is my code
txtNsg.Text = [String].Format(CultureInfo.InvariantCulture, "{0:0,0.00}", dtCustomPrice.Rows(0)("curNSG").ToString())

But Magic Wonder this one is working ,is right ?

txtNsg.Text = Decimal.Round(dtCustomPrice.Rows(0)("curNSG").ToString(), 2, MidpointRounding.AwayFromZero)
Magic Wonder 12-Oct-15 5:48am    
Surprised. Prefer your working solution.
SathishRam 14-Oct-15 1:43am    
thank u
Magic Wonder 14-Oct-15 2:00am    
Welcome.
C#
String.Format("{0:0.00}", 300.0000); // and our String.Format() will return "300.00"


-KR
 
Share this answer
 
Comments
SathishRam 12-Oct-15 5:22am    
thanks KrunalRohit, i also tried it not working
but this one s woking.is it right?
txtNsg.Text = Decimal.Round(dtCustomPrice.Rows(0)("curNSG").ToString(), 2, MidpointRounding.AwayFromZero)
Krunal Rohit 12-Oct-15 5:42am    
According to the MSDN documentation, it matches to your requirements.
If it is giving you your output, then you can use it.

-KR
SathishRam 14-Oct-15 1:45am    
thank u KR
As I can see you are not converting DB value to Numeric format. With object type you can't apply string format. You can use it in following way -

C#
if(dtCustomPrice.Rows[0]["curNDB"]!= DbNull.Value)
{
  txtNdb.Text = Convert.ToDecimal(dtCustomPrice.Rows[0]["curNDB"]).ToString("##.00");
}
 
Share this answer
 

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