James P. Wright
James P. Wright

Reputation: 9131

C# Comparing Decimal types as String

I am writing a logging system for our site and it looks at changes to Entities to see if they should be logged. I keep having a problem where the version of the Entity in the database shows decimal numbers (in string format) as "132.0000" and the current Entity has it as "132" with no decimal places. Is there a way that I can force it to either remove the ".0000" from one or add it to the other?

Upvotes: 0

Views: 3255

Answers (6)

xling
xling

Reputation: 252

You can use formatter string : F4 xxx.ToString("F4")

Upvotes: -1

Xaisoft
Xaisoft

Reputation: 46591

You can try:

if(132 == Convert.ToDecimal("132.0000")
{
    //Do Stuff
}

Replace 132 and "132.0000" with your appropriate values.

Also, if you wanted to remove the decimal portion of "132.0000", you can do:

string dec = "132.0000";

dec = dec.Substring(0,dec.IndexOf("."));

Upvotes: 1

Gregory A Beamer
Gregory A Beamer

Reputation: 17010

Provided all of the comparisons are numerics, you have to find the commonality between the two. In your post, you have already provided the answer. You need an integral number rather than a floating point number. Cast both strings to one of the integral types and compare.

Upvotes: 0

Sebastian Mach
Sebastian Mach

Reputation: 39089

Convert the entry back to decimal, I'd say.

Upvotes: 1

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174329

Hm, simply parse the value from the database into a decimal and compare it to the decimal you already have?

Upvotes: 2

Vincent Koeman
Vincent Koeman

Reputation: 751

If you always want to remove the decimal, why not cast the object to an int and then make a string of it?

Upvotes: 0

Related Questions