Solved

Parse Float Syntax?

Posted on 2004-08-30
10
508 Views
Last Modified: 2011-10-03

Hi,

ScoreNum is from a database with datatype of float. I want to convert it to an int because I can't seem to be able make calculations on it later as a float--correct me if I'm wrong.  In any case, I don't know how to parse the float field into an Int--the following does convert dr6[3] to an integer if it was of datatype was an int but I have a float so it doesn't work.  What is the syntax for float?

Thanks.

ScoreNum=Int32.Parse(dr6[3].ToString());
Response.Write(RateScoreNum);
0
Comment
Question by:champ_010
  • 5
  • 4
10 Comments
 
LVL 33

Expert Comment

by:raterus
ID: 11936214
I don't understand why you couldn't keep it as a float, you should be able to do any sort of calcuation on it.  Do you have an example of what wasn't working when you were trying that?
0
 
LVL 1

Author Comment

by:champ_010
ID: 11936416

Here's part of my code:

//This is a float coverted to string for the time being...but what do I make it so that it works in the
//IF block below:
ScoreNum=(dr6[3].ToString());

//Output to see:
Response.Write(ScoreNum);

if(ScoreNum == 1) Response.Write("1");
     else if(ScoreNum > 1 && RateScoreNum < 1.5) Response.Write("Between 1 and 1.5");
     else if(ScoreNum >= 1.5 & RateScoreNum < 2) Response.Write("Between 1.5 and 2");

etc....
0
 
LVL 1

Author Comment

by:champ_010
ID: 11936425
Sorry--RateScoreNum is supposed to be ScoreNum--just forgot to change.

Here's the partial code again:

//This is a float coverted to string for the time being...but what do I make it so that it works in the
//IF block below:
ScoreNum=(dr6[3].ToString());

//Output to see:
Response.Write(ScoreNum);

if(ScoreNum == 1) Response.Write("1");
     else if(ScoreNum > 1 && ScoreNum < 1.5) Response.Write("Between 1 and 1.5");
     else if(ScoreNum >= 1.5 & ScoreNum < 2) Response.Write("Between 1.5 and 2");

etc....
0
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
LVL 15

Expert Comment

by:Thogek
ID: 11936986
So you need to convert either the data type returned (ScoreNum) to an int, or the numeric constant 1 to a float, so that you can compare the two.  Either should work.  For example:

    int ScoreNum = Int32.Parse(dr6[3].ToString());

should work.
0
 
LVL 15

Expert Comment

by:Thogek
ID: 11936994
BTW, you can make calculations on any of float, double, int, etc.  However, *comparisons* (such as if(a == b)) generally need to be between variables of the same datatype, or the compiler will complain about type mismatches.
0
 
LVL 1

Author Comment

by:champ_010
ID: 11938616
Thank Thogek,

I tried it but I get this error:

System.FormatException: Input string was not in a correct format.

???

0
 
LVL 1

Author Comment

by:champ_010
ID: 11938996

I changed the datatype in SqlServer to decimal instead of float and the error goes away...only problem is that if the number I enter in the database is 3.66666, it only goes in SqlServer as 4--that's why I used float.  Not sure if there is a way to make the decimal datatype take 3.66--that would solve my trying to convert the float to int, otherwise I'll have to stick with float but I get the error message.

What should I do?
0
 
LVL 15

Expert Comment

by:Thogek
ID: 11943142
Ah, well, if dr6[3] is containing a float type, then you might want to do the conversion to integer (assuming you still want to do so) as something like

    int ScoreNum = System.Math.Floor((double)(dr6[3]));

(BTW, I'm assuming here that dr6 is a DataRow.)
0
 
LVL 15

Accepted Solution

by:
Thogek earned 125 total points
ID: 11943180
BTW, if I understand correctly, the default SQL Decimal datatype is decimal(18,0), which basically means a decimal with 18 digits of precision, and no digits past the decimal point -- effectively an up-to-eighteeen-digit integer.  If you want to use a SQL decimal the way you're describing, you'd want something more like decimal(10,6) or so.  (More info at http://msdn.microsoft.com/library/en-us/tsqlref/ts_de-dz_3grn.asp.)

But it sounds to me like float is more what you want, and it just determining the right conversion in your C# code to allow the comparison you wanted.
0
 
LVL 1

Author Comment

by:champ_010
ID: 11948390

Thogek,

I tried the int ScoreNum = System.Math.Floor((double)(dr6[3])); but got another error message about converting not being able to conver to double.

Then changed the datatype in SqlServer to decimal (10,6) and got some errors until I did this:

double RateScoreNum = double.Parse(dr6[3].ToString());

It works so thanks for explaining about the default decimal and changing it to (10,6)
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
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…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

831 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