Solved

string compare

Posted on 2013-06-02
8
282 Views
Last Modified: 2013-06-02
Hello,
I would like to chek if 3.5 or 5 or 5.5 or 7.8 is less then 4.
How do I do that in C#?
0
Comment
Question by:johnson1
[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
8 Comments
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39214641
Numerical values or string length. Please be more specific.
0
 

Author Comment

by:johnson1
ID: 39214648
I can have a string that is "3.5", "5", "5.5", "7.8", "8" etc.
I need to be able to compare it with the string "4" to know if it is less then 4.
0
 
LVL 63

Assisted Solution

by:Fernando Soto
Fernando Soto earned 167 total points
ID: 39214694
Hi johnson1;

You will need to convert your strings to integer data types first then do your comparison. The reason for this is for example lets say you have the following string items, "3.5", "5", "5.5", "7.8", "9", "55", the sort order of this values would be, 3.5, 5, 5.5, 55, 7.8, 9, please note the position of the value "55" it comes before the value of "7.8" numerically that is incorrect but sorting order is correct. Also the equality operators are not support in strings such as <, >, ==, <= and so on and there you would need to use the CompareTo method of the string class which will result in incorrect results as noted above.
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 44

Assisted Solution

by:Arthur_Wood
Arthur_Wood earned 166 total points
ID: 39214709
>>You will need to convert your strings to integer data types first then do your comparison<<

not to Integer data type, but to Float or Double (Integers do not have decimal points)

AW
0
 

Author Comment

by:johnson1
ID: 39214716
How do I convert "3.5" to integer? Can you please show me an example how I could do the comparison?
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39214720
Thank you Arthur glad someone is keeping me honest. ;=)

This line in my last post , "You will need to convert your strings to integer data types first then do your comparison. ", should read as , "You will need to convert your strings to float or double data types first then do your comparison."
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39214734
Hi johnson1;

As pointed out by Arthur and corrected in my last post that should be float or double. What are your string in, string variable, Array of strings, List<String> ...?
0
 
LVL 1

Accepted Solution

by:
CM2013 earned 167 total points
ID: 39214737
Use Double.Parse(yourstring) or TryParse method as well - you can handle exceptions in it.

Double d = Double.Parse(s);
if (d<4){}

Is this what you are asking?
0

Featured Post

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!

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

636 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