?
Solved

C# Formatting String to Double or Float

Posted on 2005-04-07
7
Medium Priority
?
821 Views
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

100
33.35,
55.25,
50.20
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
0
Comment
Question by:kiran_ch5
4 Comments
 
LVL 2

Expert Comment

by:jong3
ID: 13726339
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:

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

Expert Comment

by:rajaloysious
ID: 13726526
Alternatively
Convert.ToDouble, Convert.ToString etc can be used.

cheers
0
 
LVL 6

Accepted Solution

by:
melodiesoflife earned 400 total points
ID: 13732699
you can use

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

Expert Comment

by:dungla
ID: 13733828
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
}
}
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

807 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