string format 2 decimal places

how do you do string format conversion to 2 decimal places?

e.g.

convert 100 to 100.00
convert 110 to 110.00
convert 100.000 to 100.00
etc....

Thanks!
0
daydream
7/11/2005 9:07:09 PM
asp.net.getting-started 91979 articles. 3 followers. Follow

5 Replies
937 Views

Similar Articles

[PageSpeed] 53

Well, here's one way you could do it:

double value = double.Parse("100");
string result = value.ToString("f2");

This is using Numeric Format Strings (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconNumericFormatStrings.asp). There's also a class called NumberFormatInfo which gives you slightly more abstract ways to modify the string formatting. Both of these should work in other cultures, as well, so 1,000.00 will become 1.000,00 in Spain, for example.

-Scott
This posting is provided "AS IS" with no warranties, and confers no rights.
0
Scott
7/11/2005 9:54:44 PM
thank you so much. 

Is there another way to do it?
I read somewhere people use something like stringformat {0:C} or something alike to do it. I just made that up, not sure of the exact code.
0
daydream
7/11/2005 9:59:30 PM
Can you tell me what are you looking for exactly? Is there something specific in my earlier code you don't want? There are quite a few options. You could also use:

double value = double.Parse("100");
string result = value.ToString(".00");

This version uses the custom numeric format string ".00" instead of the standard string "f2". "f2" means fixed place, with 2 decimal places. ".00" means I want two placeholder digits after the decimal point. I was also able to come up with some code using the NumberFormatInfo class, but I still needed to pass in the "f" for fixed format to get it to print the decimals. The link I put in the previous post will show you a few choices.

-Scott
This posting is provided "AS IS" with no warranties, and confers no rights.
0
Scott
7/12/2005 12:45:51 AM
Thank you so much scott. Actually, your code works fine. 

I was just curious if the stringformat function can do something alike, that's all. No worries, the code is perfect.
0
daydream
7/12/2005 3:35:24 PM
Try 

String.Format("{0:f2}",value);
String.Format("{0: #,#.00",value);




Good luck ! :)
Alvin Chooi
Microsoft ASP.NET™ Enthusiast v1.1 / v2.0, Malaysia
Blog : http://alvinzc.blogspot.com
0
alvinz_c
7/12/2005 3:56:01 PM
Reply:

Similar Artilces:

Formatting a decimal to 2 decimal places
Hi - just testing some code, and the code below returns a number with 3 decimal places (eg. 450.000). Will returning a decimal always give 3 decimal places, and if so, how can I remove the last one? I don't want to use: Return String.Format("{0:C}", decRunningTotal) - as that will automatically add a $ or £ or whatever to the start, and I'd like to control that. I'd really appreciate any help, thanks, Mark Function GetItemTotal() As String objDT = Session("Cart") Dim intCounter As Integer Dim decRunningTotal As Deci...

How to get 2 decimal places?
I have one session var and value of it will be double.How can i do it to be in 2 decimal places rounded up to nearest 5cents. eg.21.781 -> 21.10 21.322 -> 21.05 45 -> 45.00 Math.Round Method (Decimal, Int32, MidpointRounding)   Rounds a decimal value to a specified precision. A parameter specifies how to round the value if it is midway between two other numbers. Namespace: System Parameters d A decimal number to be rounded. decimals The number of significant decimal places (precision) in the return value. mode Specification for how to round d...

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...

Formatting output
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   try this: budget.Text =(DR["budget"].ToString("c");  S...

formating to 2 decimal places in a GridView
sounds simple but I just cannot get anything to work I have tried many variations of these String.Format("{0:f2}",DataBinder.Eval(Container.DataItem, "A")) DataBinder.Eval(Container.DataItem, "A", "{0:F2}")  tried it in a bound column which does not work either but I want to use a textbox any help please  jnc Hello, Take a look at the following article to see if this solves your problem :) Alot of formating information :)http://msconline.maconstate.edu/tutorials/ASPNET2/ASPNET07/aspnet07-01.aspx  Hope this helps.&...

Format of the initialization string does not conform to specification starting at index 0. #2
Hey asp.net 2.0 I get this error "Format of the initialization string does not conform to specification starting at index 0." The error is related to the code I post below. this problem occured after I changed to using SqlDataSource instead of ObjectDataSource. I'm having problems (sending parameters) using ObjectDataSource so I tought I should give SqlDataSource a try. <connectionStrings>    <remove name="LocalSqlServer"/>    <add name="LocalSqlServer" connectionString="Server=asfaf;Database=adfasdfds;...

Format of the initialization string does not conform to specification starting at index 0. #2
I'm getting this error Format of the initialization string does not conform to specification starting at index 0. when I try to use the connectionString in a class.  When I use it in SqlDataSource it works fine.   Webconfig<connectionStrings> <add name="hck_cms" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\hck_cms.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient" /> </connectionStrings> My class file Dim cn As System.Data...

String to decimal with 2 places i.e. 9.999 to 9.99
Can someoen help me with this. I have a string that users are entering a decimal..I need to only show the last two decimals (BUT NOT ROUND)....so 9.999 should to to 9.99 Thanks for the help!!!!ThanksKelley Bryant Here's something I just came up with if you don't find anything better, I know lots of built-in functions like to round, so you may just need to do some string manipulations. I'd test this out though, i'm not guaranteeing it works for any case :-) Public Function TruncateDecimal(ByVal myValue As String) As String If myValue.IndexOf(".") >= 0 The...

get Format of the initialization string does not conform to specification starting at index 0. error
Hi, I have a page in which I have the following line in VB, my "abcConnectionString" is set in the web.config file. But when I click on the button, I get this error: Format of the initialization string does not conform to specification starting at index 0. What should I do? thank you.Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Dim txt1 As TextBox = Me.FindControl("TextBox1") ' txt1.Text = "i will start"Dim myConnection As New SqlConnection("abcConnectionString") myConnection.Open()D...

Showing currency to 2 decimal places in gridview gets rounded off $ 57.50 as $ 58.00
Hi I am using a gridview . the ui web page gets the data from my SQL database table the problem is the data in table has value of $ 57.50 but when the data is displayed it gets rounded off  to $ 58.00 and similarly several other data gets rounded off. How can I avoid this and display the actual amount $ 57.50 instead of rounding off??   here is the code how they are displayed in UI and now displaying as $ 58.00 // Amount_Paid fetching data from dbo.V_Payment_Amount_List //DataFormatString="{0:c}" ConnectionFinanceDB.DisconnectToDatabase();Collectin...

What are proper format strings for String.format, Number.format, etc?
Hi guys, I just started using the client side version of the Ajax framework. Everything is working out so far, except that I can't seem to be able to format any strings of numbers the way I want. Wheather I use the String.format or Number.format functions, I just keep getting Sys.FormatException errors. What are the proper format strings? I tried the relevant ones that I use in C#, but it just doesn't work.Help would be much appreciated!  I believe D, C, N, and P are the only valid ones for numbers (client side).Encosia - ASP.NET, AJAX, and more.Latest article: Using complex ...

CLR.NET datatype for decimal is System.String or Decimal
CLR.NET datatype for decimal is System.String or System.Decimal the documentation says System.string while it is mapped to DbType.decimal I need some understanding on this. any help thanks vsv ...

Differences between .net 1, .net 1.1, .net 2.0 and .net 3.0 #2
Hi, This seems to be a common question, but i havent got an answer yet:(Can, any one please explain me the differences between these versions.If you keep your feet firmly on the ground, you'll have trouble putting on your pants! There are too many differences for one email - - from 1.0 to 1.1 (not a whole lot of real change, other than fixes, at least compared to 1.1 to 2.0) With 2.0, there were many new declarative controls, with many new ideas added in With 3.0, it's a superset of 2.0 - instead of replacing the installation completely, it just 'added on' new functionality - I would...

How to format a percent number as decimal using string.format in VB?
Hi, I have a GridView-Control with labels which contain numbers in percentformat, which are formatted in the RowDataBound-Event, but when I try to update the GridView i get the Invalid String format Exception because the labels are in the percentage Format like -27,5% but i need this number as double-value like 0.275. How can I convert the number from percentage to number? Any idea?  Thanks in forward,  Vlad  Dim str as string = percentlabel.Text ' Get rid of percentage sign str = str.Replace("%", "") ' Check if you can convert dim dbl as ...

Web resources about - string format 2 decimal places - asp.net.getting-started

Penny (Irish pre-decimal coin) - Wikipedia, the free encyclopedia
The penny (1d) ( Irish : pingin ) coin was the third smallest denomination of the pre-decimal Irish pound , worth 1/240 of a pound or 1/12 of ...

Dewey Decimal Classification - Wikipedia, the free encyclopedia
This highly organized system categorizes books on library shelves in an efficient, specific and repeatable order that makes it easy to find any ...

Decimal Rounding on the App Store on iTunes
Get Decimal Rounding on the App Store. See screenshots and ratings, and read customer reviews.

Dewey Decimal Tattoo - Flickr - Photo Sharing!
Children's librarians represent.

Math of the Day: Sesame Street’s The Count Reads Pi to 10,000 Decimal Places
... has created a true monster out of your favorite Sesame Street vampire, with audio of The Count reading every digit of Pi out 10,000 decimal ...

Paul Harapin heads Decimal Software after VCE ousting
Former VCE Asia-Pacific boss, Paul Harapin, has taken over as CEO of Decimal Software.

Feeling green: the dangers of decimal currency
The curious case of possibly toxic coins, via Austria and a devastatingly original idea for Canberra.

Jobless Rates With Another Decimal
The BLS reported a jobless rate of 8.3% for both January and February. Adding an extra decimal, the unemployment rates would actually be 8.26% ...

JPMorgan Sued by Trader Over $3 Million Decimal Point
JPMorgan Chase & Co. is being sued by a trader who says he accepted a contract from the investment bank because a typographical error made him ...

.Decimal Archive - Houston Gamer
It was only back in August when newly-formed Houston game studio Orb Interactive launched a Kickstarter campaign for “.Decimal”, a sci-fi take ...

Resources last updated: 11/21/2015 10:00:24 AM