Solved

Parse Float Syntax?

Posted on 2004-08-30
10
505 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
 
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

708 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

15 Experts available now in Live!

Get 1:1 Help Now