Solved

Parse Float Syntax?

Posted on 2004-08-30
10
512 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

734 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