Uladz Kha
Uladz Kha

Reputation: 2354

How to compare two strings that represent numbers in c#?

I have two strings "73248723847239847283974283749238" and "98231912938129381290120378988945" They contain numbers and may be 50 chracters long. I found just one solution: convert it to array of numbers and compare sums of it but it is not very good from performance side. Does anybody know how i can compare it?

Upvotes: 1

Views: 94

Answers (2)

VDN
VDN

Reputation: 737

Make it the same length with PadLeft() and then comapre it:

var s1 = "73248723847239847283974283749238";
var s2 = "98231912938129381290120378988945";

s1 = s1.PadLeft(50, '0');
s2 = s2.PadLeft(50, '0');

var compareResult = s1.CompareTo(s2);

Upvotes: 3

fubo
fubo

Reputation: 45947

I have to compare it and find biggest one of it.

You could handle it with BigInteger

BigInteger b1 = BigInteger.Parse("73248723847239847283974283749238");
BigInteger b2 = BigInteger.Parse("98231912938129381290120378988945");

BigInteger result = BigInteger.Max(b1, b2);

convert it to array of numbers and compare sums of it but it is not very good from performance side

Side note - performance is the smallest problem of this approach 😂

Upvotes: 6

Related Questions