开发者

why does toString() cause my string to lose a decimal place?

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.R开发者_开发问答ows.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


Try .ToString("N2")


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.


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 


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


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


You want:

String.Format("{0:0.0}", 9.2);   //9.2
String.Format("{0:0.00}", 9.2);  //9.20
String.Format("{0:0.000}", 9.2); //9.200


I've found in instances of passing the variable betweeen classes, before ToString, it is possible the loss is there and that ToString is accurate, as it usually is, for example if you wanted to test its accuracy, just divide 10 by 3 and ToString it, it will bring recurring decimal places. Below is an example of a similar issue I was having and how I worked around it, though I suspect there is a slicker way that mine.

Example

class MyDecimal
{
 public decimal Number = 138 / 10;
}

elsewhere doing

MyDecimal ThisDec = new MyDecimal();
public decimal ThirdPlaceIsNoGone = ThisDec.Number / 100;

I ended up instead of dividing by 100 after gathering the decimal, I needed to divide by 1000, the issue looked to be that ToString was dropping the 3rd decimal place, but in fact it wasn't being pulled between classes to begin with

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜