Solved

string compare

Posted on 2013-06-02
8
281 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

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…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

751 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