Click here to Skip to main content
11,477,625 members (75,275 online)
Rate this: bad
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 23:27pm
Rate this: bad
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!
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
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
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 Sergey Alexandrovich Kryukov 274
1 Richard Deeming 245
2 _duDE_ 165
3 Tomas Takac 150
4 Sascha Lefèvre 145
0 Sergey Alexandrovich Kryukov 7,675
1 OriginalGriff 7,246
2 Sascha Lefèvre 3,034
3 Maciej Los 2,491
4 Richard Deeming 2,305

Advertise | Privacy | Mobile
Web03 | 2.8.150520.1 | Last Updated 10 Apr 2012
Copyright © CodeProject, 1999-2015
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