tags:

views:

103

answers:

6

Hi all

Just to be forward, my .net and c# expierence is very limited, that one could say I'm a complete noob :) However I do have some grasp on OOP as I usually work in PHP (FWIW).

My problem seems to be with this line:

Session["total"] = dt.Compute("SUM(price)", "").ToString();

which will output a total of say "9.2" where as the price is "9.20" from a few record lines as such:

dt.Rows.Add(strRecord, 9.20, dropDownListDistrict_stepTitle.Text);

Thus 9.20 + 9.20 = "18.40", but the page displays "18.4".

The label that displays the total sum is:

<asp:Label ID="labelTotal3" runat="server" Text="0" />

From what I've gathered, I'm suffering from some kind of type casting problem?

Hopefully I haven't missed anything, its a bit difficult to show all the code but hopefully someone can point me in the right direction?

Question is; how can I get an extra decimal place to show up on my sum (or why is it dropping the last decimal place?).

TIA Jared

+2  A: 

Try .ToString("N2")

Jonathan
+1  A: 

You're evaluation of the problem is incorrect

9.20 + 9.20 = 18.4 (the trailing zero will be dropped) therefore the display will automatically be "18.4"

Try something like this

string.Format("{0:C0}", myNumber)

You could also go really fancy and swap out a currency symbol, add/remove the number of decimal places, etc, using the following Function (Sorry it's in VB). The idea here is not that it's always applicable, but just another option if you're looking for more flexibility.

Public Shared Function CustomFormatCurrency(ByVal input As Decimal) As String 
    Dim nfi As System.Globalization.NumberFormatInfo = New System.Globalization.NumberFormatInfo  
    nfi.CurrencyDecimalDigits = 0  
    nfi.CurrencySymbol = "$" 
    Return String.Format(nfi, "{0:C}", input)  
End Function 
rockinthesixstring
How is 18.40 != 18.4?
BoltClock
@BoltClock ...for very small values of 0... but that aside, I think the point is rockinthesixstring was trying to make was: a string is not a number, only a printable representation of it.
pst
@pst: lol I knew that was coming.
BoltClock
the trailing ZERO is meaningless in the number, so if the number has a trailing zero, it will be dropped.
rockinthesixstring
@rockinthesixstring: which is what I said.
BoltClock
... added to my answer.
rockinthesixstring
Sorry if this is a dumb question or I fail to understand (maths isn't my strong point either ha!) but why does the string drop the last decimal place? I could understand if it were printing an interger answer but its just a string? I'm guessing the conversion drops the decimal place even though the outcome is a string? Did I make sense here? :P
Jared
It's a string representation OF the calculated value. The zero is strips from the value before it's converted to a string.
rockinthesixstring
Thanks for the extra explanations, I see.
Jared
A: 

The 0 is mathematically insignificant (i.e. 18.40 = 18.4), which is why it's being dropped from your string. As Jonathan says you'll need to pass "N2" to the ToString() call so that it formats with two decimal places.

BoltClock
A: 

Nothing is really getting lost, since 18.4 is exactly the same thing as 18.40 (to the computer, that is...). To fix it, you need to specify the format:

.ToString("n2");

This formats your value as a number with 2 digits to the right of the decimal

Ray
+2  A: 

Thus 9.20 + 9.20 = "18.40", but the page displays "18.4".

18.4 = 18.40. Trailing zeroes add no meaning to a number, so unless you ask for them to be printed, they won't. See String Format for Double for different ways in which you can format the number as per your requirement.

casablanca
A: 

Try ToString("##,#.##") or string.Format("{0:##,#.##}")

amkh