Jump to content
Xtreme .Net Talk

Recommended Posts

Posted
How can I get an integer NOT to round? i.e. 1.88 I want to be 1 and not round up to 2. Or should it be a double? If so, I do I now show the decimal places?

Thanks,

Tehon

  • *Experts*
Posted

In C# you just cast as an int, and it never rounds. Try:

decimal i = 1.88M;
Debug.WriteLine((int)i);

 

I don't know the equivalent in VB.NET, if you need it just ask.

 

-nerseus

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
  • *Experts*
Posted

You guys are mostly right - assuming you are using positive numbers. Floor behaves differently for negative numbers. If you do Math.Floor(-1.88) you'll get 2 (weird, but true). To do that properly using the math functions, you need to check for < 0 and use Math.Ceiling. Casting as (int) works in both cases.

 

-Ner

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...