C# Formatting String to Double or Float

Posted on 2005-04-07
Last Modified: 2012-06-21
I am learning C#.... Need some help

I have some data coming from database... I have to loop through the dataset and bind to those Values to asp:lables in xx.xx format (33.54,14.56 etc).. only two numbers after the decimal point

foreach(DataRow drow in ds.Tables[0].Rows)

Stringvalue= drow["ColumnName"].toString();

My String Values are coming like this

66.38 etc

I have to deduct these Values from 100 like
100-50.20 = 49.80...

How Can I do that in C#... Final output should be in xx.xx format.. I tried 100-Stringvalue .. It didnot work
Question by:kiran_ch5
    LVL 2

    Expert Comment

    You will need to cast your string into a double... This will cause problems if your string contains non numerical characters.  In order to cast, you may do the following:

    Double NumValue = Double.Parse(drow["ColumnName"].toString());

    Better way of doing this is by putting the above into a try/catch, in case the input is not numeric:

      Double NumValue = Double.Parse(drow["ColumnName"].toString());
    Catch (Exception e)
       MessageBox.Show("Invalid input");
    LVL 8

    Expert Comment

    Convert.ToDouble, Convert.ToString etc can be used.

    LVL 6

    Accepted Solution

    you can use

       double d = double.Parse(StringValue);
       d = 100 - d;
       // Format xx.xx
       string str = string.Fomat("{0:##.##}", d);
    // ....
    LVL 13

    Expert Comment

    You can use something like that
    foreach(DataRow drow in ds.Tables[0].Rows)
    if ((drow["ColumnName"] != null) && (drow["ColumnName"] != DBNull.Value))
    double d = double.Parse(drow["ColumnName"].ToString());
    d = Math.Round(d, 2); // round the number with 2 number after digits

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
    Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now