Solved

Parse Float Syntax?

Posted on 2004-08-30
10
513 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
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!

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

623 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