Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Parse Float Syntax?

Posted on 2004-08-30
10
Medium Priority
?
514 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 500 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

Technology Partners: 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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Suggested Courses

715 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