Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
i have amount field in database which is to be displayed in textbox. Now what i want is that it should contain only 2 digits after decimal point. how can i format this??
Posted 9-Apr-12 22:27pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Use ToString() overload where you can specify a format. So it will be like -
decimalValue.ToString ("#.##");
 
Hope this helps!
  Permalink  
Comments
Member 8589824 at 10-Apr-12 4:39am
   
what should i write in place of #.##?
Ankur\m/ at 10-Apr-12 4:51am
   
Don't replace that with anything. Copy it as it is.
VJ Reddy at 10-Apr-12 4:56am
   
Good answer. +5
vino2012 at 10-Apr-12 6:35am
   
'#' means number format....
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

The solution 1 given by Ankur is good.
I want to add that if you want to force 0 to be displayed when there are no numbers after the decimal point and also to display a comma separator, the following format can be used
TextBox1.Text = Amount.ToString("#,0.00");
//The format will be
//3 ->  3.00
//3.4 ->  3.40
//.5 -> 0.50
//4594.3 -> 4,594.30
  Permalink  
Comments
Ankur\m/ at 10-Apr-12 5:02am
   
I missed that part, thanks for pointing out.
VJ Reddy at 10-Apr-12 5:26am
   
Thank you.

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

  Print Answers RSS
0 OriginalGriff 6,649
1 Sergey Alexandrovich Kryukov 6,270
2 CPallini 5,190
3 George Jonsson 3,574
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 10 Apr 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100