Formatting numbers -- maximum decimal places?

Stupid number-format question here: I want to format some prices with either zero or two decimal places.

19.9000 -> $19.90
19.9900 -> $19.99
20.0000 -> $20
The only way I can think of doing this is:

dim dTemp as decimal
dTemp = Decimal.Round(dInput, 2)
if Decimal.Truncate(dInput) = dTemp Then
return "$" & Decimal.Truncate(dInput).ToString
else
return "$" & dTemp.ToString
end if

Is there a better way?
0
noobjk
4/6/2004 3:22:41 PM
asp.net.getting-started 91979 articles. 3 followers. Follow

2 Replies
588 Views

Similar Articles

[PageSpeed] 38

have u tried somethign like:


String.Format("{0:c}", dInput);
Bilal Hadiar, MCP, MCTS, MCPD, MCT
Microsoft MVP - Telerik MVP
0
haidar_bilal
4/6/2004 3:44:45 PM
Thanks for the suggestion. That deals with the first two cases:

19.9000 -> $19.90
19.9900 -> $19.99
....but not the third:
20.0000 -> $20
0
noobjk
4/6/2004 4:12:39 PM
Reply:

Similar Artilces:

how to format number to 4 decimal places
I have a decimal type number in my datagrid that I want to format to 4 decimal places. I tried <asp:BoundColumn ... FormatString="{0:n}" ..../> It give me only two decimal places: 0.28 How can I format my number to display as: 0.2833 (4 decimal places)? Check out this link: http://programmers-corner.com/viewSource.php/123 Met bet is: Console.WriteLine("Number formatting to 4 decimal places: " + theDecNumber.ToString("N4")); HighOnCodingWanna get high!  Try theDecNumber.ToString("###0.0000"); http:/...

Format Number to two decimal places problem
Although it should be pretty straight forward, I am having a problem dividing two numbers in two textboxes and displaying the result, formatted to two decimal places, in a third textbox - Box3 This is what I used but the end result still has many decimal places. Box3.Text = String.Format("{0:c}", (Convert.ToDecimal(Box1.Text) / Convert.ToDecimal(Box2.Text))) Hi there, you can do this: Box3.Text = String.Format("{0:c2}", (Convert.ToDecimal(Box1.Text) / Convert.ToDecimal(Box2.Text))) At least in C#, but the number after the C means the number of decimal...

How to format a number to remove unnecessary decimal places?
My db query is returning numbers with decimal places. i.e. 12.00, or 14.50 What function can I use so they are displayed as 12 and 14.5? (vb.net please)ThanksMarkUsing www.HostingUK.net in the UK, and www.QuadraHosting.com.au in Australia for my ASP.NET hosting. Check out Standard Numeric format stringsSushila Bowalekar PatelVisual ASP/ASP.NET MVPhttp://weblogs.asp.net/sushilasb Here's what I've decided to use. It seems to take care of all my issues.Label1.Text=CType(reader("AnyColumnFromDB"), Single).ToString("G")(I'm using a datareader here, but other db/dataset syntax should work th...

European (German) number formatting
Hello, we are developing a PB65 application using PFC and Cornerstone running on NT. I datawindow columns representing decimal numbers we would like to use the German formatting, that is a comma as a decimal delimiter and a period is representing thousand. As mentioned in the online books I used the the U.S. notation for numbers in the dadatwindows display format and edit styles and changed the Systems national settings to German. If I'm trying to save a datawindow containing a number with decimal places, e.g. 123,51, only 123,00 is stored in the database. This case is true...

Bug : Loosing decimal places on European number format in PB7
Hello, We use PB7 and ms sql server 6.0. In our Windows 95 configuration, the decimal point is a comma. For datawindow columns representing decimal numbers we would like to use the Euopean formatting, that is a comma as a decimal delimiter and a period is representing thousand. When you type in 123,45 only 123,00 is represented in de datafield when you leave the field. The problem only occurs when you have fields of data type 'float' ( double ). For fields of type 'real ' the reaction is correct. Early versions of pb65 had the same problem for fields of type 'r...

Format a number to contain commas and to remove decimal places if = .00.
HI there, I am binding data to a gridview & I would like to format a number so that it contains commas and also maintains the numbers after the decimal point BUT if there is 00 after the decimal point it would be removed. Note I dont want to round the number to the nearest integer or to fix to a specifc number of decimal places i.e  1000.00 as 1,000   1000.5  as 1,000.5 1000.66 as 1,000.66  1000.666 as 1,000.666    The best I can come up with is   DataFormatString="{0:n}" but then I would be left with 1000.00 and fi...

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

Trying to get and display a number as it is in the DB (with two decimal places), not rounded
Hi,I am running a command in my aspx page that calls a SP that retrieves data from the DB. But the numbers stored in the DB with several digital places are displayed in the web page rounded and without decimal places.Sub show_Q(of_num)   Dim CmdShow As New SqlCommand("detail_Q", strConnection)  CmdShow.CommandType = CommandType.StoredProcedure   CmdShow.Parameters.Add(New SqlParameter("@Offer_id", SqlDbType.bigint))  CmdShow.Parameters("@Offer_id").Value = of_num   CmdShow.Parameters.Add(New SqlParameter("@Caliber_Q", SqlDbType.Decimal, 5))  CmdShow.Parameter...

Change the number of decimal places in a label number?
I have a small web form that calculates a rate. How do i change so the output label being used doesnt show more than two decimal places? Try this   Dim x As Decimal = 12.35468 Dim y As Decimal = 13.2654 Label1.Text = (x * y).ToString("#,###.##")   HTHPlease remember to "Mark As Answer" if this post answered your question!Bilal Shouman - MCAD.NET|My Blog|--------------------------------- Dim STR As String Dim VALUE As Decimal = 12.5555555555STR = String.Format("{0:#.###,###}", VALUE) Label1.Text = STR   THE OUTPUT OF THIS ST...

VB.NET: How to convert hexadecimal number to normal (decimal) number?
Hi to all VB.NET function Hex() converts numbers to hexadecimal strings, is there any reverse function that converts hexadecimals back to numbers?? Thanks, Hooker Hi, how are you? In C#, we have something like: Convert.ToInt32(string strValue, int Base)... So I basically can do: Int iDecimal = Convert.ToInt32("AF8", 16); I'm pretty sure there must be something like it on VB.Net. Hope this Helps, Covohttp://covodev.blogspot.com/"Everything should be made as simple as possible, but not simpler", A. Einstein That should work, thanks. I'm fine, BTW...

inserting a decimal number from.net to stored proc. with decimal parameter
I'm trying to enter large number 99999999, (8 digits) converting to decimal in .NET, then using that as a parameter in stored proc. which has a decimal type (decimal(15,2) as a parameter. C#:if (txtAnnualizedSaving.Text.Trim() != string.Empty) {try { sdata.AnnualizedSaving = Convert.ToDecimal(txtAnnualizedSaving.Text); // only for testing ....scale = (System.Decimal.GetBits(sdata.AnnualizedSaving)[3] >> 16) & 31; }catch { sdata.AnnualizedSaving = 0; } }   ... In the business object I have the following code:private bool UpdateData() {bool ret = false; try ...

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

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

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

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

Web resources about - Formatting numbers -- maximum 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: 11/29/2015 9:20:17 PM