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

x
?
Solved

string compare

Posted on 2013-06-02
8
Medium Priority
?
284 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 64

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 64

Assisted Solution

by:Fernando Soto
Fernando Soto earned 668 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
Industry Leaders: 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 44

Assisted Solution

by:Arthur_Wood
Arthur_Wood earned 664 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 64

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 64

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

670 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