Click here to Skip to main content
12,348,594 members (50,968 online)
Rate this:
 
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 10-Apr-12 4:39am
   
what should i write in place of #.##?
Ankur\m/ 10-Apr-12 4:51am
   
Don't replace that with anything. Copy it as it is.
VJ Reddy 10-Apr-12 4:56am
   
Good answer. +5
vino2012 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/ 10-Apr-12 5:02am
   
I missed that part, thanks for pointing out.
VJ Reddy 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
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160621.1 | Last Updated 10 Apr 2012
Copyright © CodeProject, 1999-2016
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