Formatting output - 2 decimal places

Hi, I'm recovering data from an SQL table using C#.

I have an object that is DR["budget"] I can place this into a text box using: budget.Text = DR["budget"].ToString();

but it displays as 5000.0000 even though the column in the table is declared as 'Money' (how many currencies use 4 dec places?)

How can I get it to display as just 5000.00 ?

I've tried a variety of suggestions from the web, but none seem to get anywhere close.

Regards, Bernard D

0
Beady999
4/6/2009 3:38:27 PM
asp.net.getting-started 91979 articles. 3 followers. Follow

11 Replies
611 Views

Similar Articles

[PageSpeed] 18

 

try this:

budget.Text =(DR["budget"].ToString("c");

 


Sri
Please mark as answer if it helped you
0
jsriharsha
4/6/2009 4:05:55 PM

If you want to format as a currency use this:
String.Format("Budget: {0:C}", value);

if you only want it as a number with 2 decimals and dont want a currency symbol use this: 
String.Format("Budget: {# ##0:00}", value);
 
0
toas1
4/7/2009 6:30:00 AM

 Make the column in database decimal(18,2) and you are done.


Always "Mark as Answer" the Post That Solves the problem.Because It helps others to find the solution.
Mohammad Hussain
http://mohdhussain.blogspot.com/
0
mohd786hussain
4/7/2009 6:38:58 AM

Hi,Beady

 U can Refer for that

http://stackoverflow.com/questions/164926/c-how-do-i-round-a-decimal-value-to-2-decimal-places-for-output-on-a-page
http://blog.stevex.net/index.php/string-formatting-in-csharp/
http://www.dreamincode.net/forums/showtopic64349.htm
http://www.geekpedia.com/KB10_How-can-I-format-a-number-to-x-decimal-places.html


RAGHAV

MVP ASP/ASP.Net Read My Blog


MARK THE POST AS ANSWER IF IT HELPS U.


"Success doesn't come to you…you go to it."--Marva Collins




"Success does not come to those who wait . . . and it does not wait for anyone to come to it." Anonymous


0
raghav_khunger
4/7/2009 6:47:11 AM

mohd786hussain:
Make the column in database decimal(18,2) and you are done

Why do it like that? The number of decimals to display is presentation stuff and should be handled in the presentation layer. I know this is about storing a currency but there there might be many times when more that 2 decimals are needed (for calculation, currency conversions etc).

0
toas1
4/7/2009 6:53:52 AM

toas1:

mohd786hussain:
Make the column in database decimal(18,2) and you are done

Why do it like that? The number of decimals to display is presentation stuff and should be handled in the presentation layer. I know this is about storing a currency but there there might be many times when more that 2 decimals are needed (for calculation, currency conversions etc).

 

i think you know the answer with this line :

I know this is about storing a currency


Always "Mark as Answer" the Post That Solves the problem.Because It helps others to find the solution.
Mohammad Hussain
http://mohdhussain.blogspot.com/
0
mohd786hussain
4/7/2009 7:10:40 AM

 Many thanks for the responses.  I have now managed to get the solution I need.

 budget.Text = Convert.ToDecimal(DR["budget"]).ToString("N2");

 seems to do the trick.

Regards,

Bernard D

0
Beady999
4/7/2009 8:03:29 AM

sorry, didnt mean to hurt your feelings Embarrassed. I am not a database gay so I am sure how you guys usually store currency values. My only point is that formating values is something that I usually do in my presentation layer and that not all currency values are ok to store with only 2 decimals... 

0
toas1
4/7/2009 8:09:52 AM

Beady999:

 Many thanks for the responses.  I have now managed to get the solution I need.

 budget.Text = Convert.ToDecimal(DR["budget"]).ToString("N2");

 seems to do the trick.

Regards,

Bernard D

 

 you can also use N1, N2 and N3,N4 and son on.


We All move to our original place.
Don't forget.

Imran Baloch
0
imran_ku07
4/7/2009 8:33:53 AM

THanks it worked for me....


sudha sankar
0
sudhasankar
4/7/2009 8:50:23 AM

Hi Pleaes find teh solutation.

Pur ther currency symbole in the "Text" tag e.q. [Text ='&euro<%# eval(....)%>'].

<asp:Label ID="Label1" runat="server" Text='<%# Eval("MatchOrder","{0:F2}") %>'></asp:Label>


Thanks & Regards
Jaiswar Vipin Kumar R.
Mumbai - India
Contact : +91 977 359 6947
+91 986 956 6849
0
jaiswarvipin
4/7/2009 10:19:26 AM
Reply:

Similar Artilces:

Formatting decimal places...
I have an application which pulls data (a currency amt) from an Access db. The number is a subtotal from a purchase. This number has 2 decimal places which is correct for currency. The application then performs some functions to display the tax amount and then another function to display the total amount due. The tax and total due figures both have 3 decimal places, obviously a result of the mathematical functions being performed on them. I am presenting these numbers in the form of a label on my form. I know this is silly but how can I present the tax and total due figures with only 2...

Format decimal places
I have the following expressiona3 = GetNomBal(period, "40011") GetNomBal is a function which returns a "double" value, I want a3 to display at all times as with two decimal places, at the moment if the function returns 361.23 then a3 displays as 361.23 which is what I want, But if the value is say 200.00 then it displays 200 I tried a3 = Format(GetNomBal(period,"40011"),currency) but got an error. Can anyone help Thanks :)Ben Durkin MCP MCSA MCSE, Currently learning VB.NET and ASP.NET Burnley, England Try this, MyStr = Format(334.9, "###0.00");My ASP.NET blog ~God doesn't play dice....

Formatting decimal for output
I need to display the data as currency. I have a Data Reader with a currency field, and I need to display with only two decimals and with comma separators for the thousands and millions. How can I do that? Hi !! I did something similar in C++ using the remainder so you get the remainder of the million then you output the number and the comma then the remainder of the thousands with comma. I never tried it in .NET and i do not know if there is a remainder funtion to use in VB or C# Fadil Alnassar www.fadilalnassar.com | FREE Nodil Tab Controlhttp://www.mefranchising.com you can use...

how to get start with .NET
the net help people installed  to my desktop 1-microsoft visual studio .NET enterprise developer 2003 2-microsoft .NET framework 1.1 what else do i need ...???  and how can i try with some simple code in VS.Net and run it to see wat happen...????? please help If you want to develope web applications, then you'll also need IIS on your PC.  That'll require XP-Professional or Windows 2000/2003, but XP-Home edition won't allow IIS installation. As for how to get started, you can search the web for some tutorials or buy a good book on VS.  The topic is way too vast to...

Getting Start With .net
hi i'm intersted to learn asp.net (vb or delphi) . i programming with delphi6 but i'd like to tp know something about programming in internet specially with .net . i hear about microsoft.net with delphi 7 that i can programming with delphi in .net .... is it right ? what 's our opinian about it ? and better to learn Vb.Net or continu with delphi(or delphi.net) thanks. Ali Darabian I think there is some support for Delphi in .Net. However, you will find much more support from the user community if you choose a more popular language such as VB.Net or C#. Since your al...

Where to start and how did you get started?
Hi, I have had a feeling for a long time to jump into the adds market for some time due to my low income (shooting for the stars) and I don't have anything better to do these days. But I find it really difficult not to give up due to lack of guides (what I can find atleast) and all the hard words which only makes me confused. But I won't give up. Never. Not sure if this is the right place to post this, but I deffinitly would be really greatfull for help getting started. How did you get started? +If there are any teacher that wanna show me the ropes private, I can give payment with ...

How to format a string of percentage format to decimal format ?
How to format a string of percentage format to decimal format ? 1    orginalValue  // value will be 1265.48 2    percentString // value after applying percentage format will be 126548 %3    requiredvalue // value should be 1265.48  I converted the string from orginalValue to percentString, now i need to convert back to requiredvalue. Is there any way to do ? regards, Ananth RM - msarm.com  string percentString="126548 %";double value=double.Parse(percentString.Replace("%","").Tri...

Getting started .net website
Hi folks,Does anyone here know of a good website to just sit back and read for learning .net? Thanks in advance.Baldeep Right here... Click the Get Started and Learn sections here. Quite a few out there - - this one, has a lot of good videos -  also ASPNet101.com, but then, I might be a little prejudiced David WierMCP/ASPInsiderASPNet101.com - where to look first!Please Vote for ASPNet101 - 'Best Community Resource'!Control Grouper - easily control properties for multiple controls with one control!Calendar Express - The Best HTML Calendar Generator on the web!(P...

How do I get started with AJAX.Net?
Hi All, I'm a seasoned asp.net developer but have not yet ventured into atlas et al.  I prefer books, but will take whatever I can get if it's good and will get me going. Any recommendations? Thx in advance. Jeff try here first http://ajax.asp.net/downloads/default.aspx?tabid=47 then learn from sample and docJames Wu (MIB426).NET is only way to goMCP, MCSE, MCDBA, MCSD, MCAD I find the materials online display features and implementations but are lacking architectural depth, and am seeking more comprehensive resources. By architectural depth, I mean that I cannot watch ...

formating decimal places in a dataset
I am pulling a decimal value and then exporting it to a textfile. I know how to format if I was doing it on a webform - no prob, but pulling the info otherwise is causing me some problems and I was wondering if anyone can help Sample Code: Dim numberPlaceHolder as string Dim iLoop as int Dim dataSet1 as dataset .... fill dataset1 .... For iLoop = 0 To dataSet1.Tables("tmpDecimal").Rows.Count - 1 numberPlaceHolder = dataSet1.Tables("tmpDecimal").Rows(iLoop).Item(1) Next the numberPlaceHolder needs to be formate...

Getting Started with .net and a few questions
Hello I've done some asp and php programming along with various other languages in the past and slowing getting my head around .net.I have a small project of creating a Holiday Booking system on our Intranet and just trying to get the first part to work properly will hopefully get me sorted for the whole project. The Intranet is running on .net 2 and I prefer to use the VB code rather than C#. This is connecting to an SQL server.My initial page that I am trying to create is just for an admin user to view which dates in a table have been specified as Bank Holidays and be able to add ...

Getting started with JScript.NET
I'm a Javascript and Actionscript developer, and the thought of being able to do server-sides in something I know was too good to resist. Does anyone know some good tutorials for developing ASP.NET pages in JScript?  I can find lots of console code, but simple things are hard: like I can't even figure out how to print something out to the web page (just a string). What I'm really trying to do is access a database, verify a login, and print JSON out to the response.  What should I do to start?  Is there a better way to do this? (IE: using some JSON library already?). I'm commun...

plz Help: Converting 4 decimal places to 1 decimal place
Hi,I would like to convert 4decimal places to single(1) decimal place.For that i wrote below code. Its working sometimes. ex1:Input: StrAnlPct = "3.0000,2.9833,3.6546,4.65+6,5.54986,81.dsfds"Output: "3.0,2.9,3.6,4.6,5.5,81.d"For first example its working,Ex2:Input: StrAnlPct = "476290.4762,120.0000"For this i m getting error. Error is:    "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: startIndex "And    Source Error: At Line 590Line 588:            &...

Format the value to Two places of Decimal
With the following code I am unable to display the value of sSI in 2 places of decimal. Could you please let me know how to display the value in 2 places of decimal <div id="ScheduleInterchange" style="POSITION: relative"> <FONT style="FONT-SIZE: 10pt; COLOR: #330099; FONT-FAMILY: Arial"> Sch</FONT></a><span id="aceScheduleInterchange">Calculating Ace</span> <FONT style="FONT-SIZE: 12pt; COLOR: #330099; FONT-FAMILY: Arial"> ...

Remove Decimal Places From Currency Format
Does anoyone know how to remove the trailing zeros from a currency formated string: for example in the showad.aspx.c there is  a line which show the price of an item using this code: AdPriceLabel.Text = String.Format("{0:c}", ad.Price); if the value on the database table is 5 then it is diaplyed on the web page as:  $5.00  All the numbers in my datbase are whole numbers (no decimal places).Custom Classifieds Site: http://tinyurl.com/3ybh5k Inspect the Current Number format System.Threading.Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyDecimalDigits; Yo...

Web resources about - Formatting output - 2 decimal places - asp.net.getting-started

Wikipedia:Manual of Style/Text formatting - Wikipedia, the free encyclopedia
The most common use of boldface is to highlight the article title, and often synonyms, in the lead section (first paragraph). This is done for ...

Formatting « Above the Law: A Legal Web Site – News, Commentary, and Opinions on Law Firms, Lawyers ...
Above the Law: A Legal Web Site – News, Commentary, and Opinions on Law Firms, Lawyers, Law School, Law Suits, Judges and Courts

Android Studio 1.2 Reaches Beta, Adds Built-In Decompiler, Inline Debugger Variables, Smarter Formatting ...
When Android Studio v1.1 entered the Stable channel, about 6 weeks ago, the Dev Tools team gave word that v1.2 was already well underway and ...

TextExpander 2.0 Brings Rich Text Formatting, Fill-In Snippets And More
TextExpander has finally received its big 2.0 update.

WordEver HD - Text Editor with Redesigned Keyboard and MarkDown syntax & formatting highlight
Holen Sie sich „WordEver HD - Text Editor with Redesigned Keyboard and MarkDown syntax & formatting highlight“ im App Store. Sehen Sie sich Screenshots, ...

Macs are vulnerable to Thunderstrike 2 firmware malware that survives formatting
... Thursday, researchers will demonstrate that not only can Macs be remotely infected with malware, but that this malware can survive a user formatting ...

16 Tips on Blog Writing and Formatting
... again. My point is that you ought to take advice with a grain of salt. And that applies to this post as well. Writing blog posts and formatting ...

Select Text With Similar Formatting in Google Docs
Google Docs added a cool new feature that lets you select all the text with similar formatting, so you can quickly make some changes. It's like ...



Resources last updated: 12/30/2015 8:48:06 AM