PassionateDeveloper
PassionateDeveloper

Reputation: 15138

How to cut off the end by converting double to int?

I think you will loughing at me, but I need help here...

I have a double (4.5234234) and need this in Int, but everytime without the numbers after the points. So it doesn't mathers if its 4.01 or 4.99 it should ever be 4 in Int.

how to do that?

Upvotes: 3

Views: 3196

Answers (4)

Mitja Bonca
Mitja Bonca

Reputation: 4546

You can use Convet method:

double d = 5.555;
int i = Convert.ToInt(d);

Mitja

Upvotes: 0

Anders
Anders

Reputation: 878

a nasty but workable way is i guess to round it first using

          double d = 5.555;
          int a = (int)d;

Upvotes: 0

gor
gor

Reputation: 11658

You can use Math.Truncate method.

Return Value

Type: System.Double

The integral part of d; that is, the number that remains after any fractional digits have been discarded.

Upvotes: 4

Øyvind Bråthen
Øyvind Bråthen

Reputation: 60714

Have you actually tried this?

double myDouble = 4.5234234;
int result = (int)myDouble;
//at this point result is 4

This will "strip" everything after the decimal point, and only return the whole number part of the double.

Upvotes: 9

Related Questions