tags:

views:

457

answers:

6

example, I have number 345.38 , 2323.805555 , 21.3333 . i want to get the number after the decimal and round it up.

345.38 --> 4

2323.805555 --> 8

21.3333 --> 3

Thanks

+7  A: 

multiply by 10

ceiling (always rounds up, use 'round' to round down if lower than 0.5)

find the result of modding by 10

Like:

float myFloat = 123.38f;
float myBiggerFloat = Math.Ceiling(myFloat * 10.0f);
int theAnswer = ((int)myBiggerFloat % 10);

Or just ask for help for your homework on SO, either way seems to work.

mmr
Watch for overflows!
Jason
good point; if he's near the edge of usefulness for floats. And if he's got a float value that's being expressed in scientific notation, does he still take the number after the decimal point, even though it's not the tenth's place?
mmr
Worrying about overflows on a floating point type by multiplying by 10 is a bit of a stretch. Decimal is the one with the lowest range and it's maximum value is still 7.9*10^28. In the case of float, I'd be more worried about significant digits and not about overflow.
Thorarin
Math.Ceiling returns "4" for the last use case, not "3". Use Math.Round instead.
scwagner
He said he wants to 'round it up', but he's rounding down in the last case. That's why I added the comment about using round vs. ceiling.
mmr
He's rounding up in one case and rounding down in two. I think he must have just meant "round off."
Nosredna
For those worrying about overflow in a float (oh brother), you can do myFloat-floor(myFloat) first.
Nosredna
The overflow concern comes from the cast to int, not the arithmetic.
Jason
You have to cast to int to use modulus. You could use a combination of ceiling/round and floor in order avoid going to an int, I suppose.
mmr
"The overflow concern comes from the cast to int, not the arithmetic." Oh! OK, that makes sense.
Nosredna
+4  A: 
Jason
I would definitely *not* submit this answer. Especially not if the assignment doesn't mention the length of the numbers that have to be supported.The routine working for numbers up to 27 digits long instead of 28 is not likely to be a problem. Your solution breaks depending on a user's culture setting (not all countries use .)
Thorarin
Formatting a number into a string is not fast, it's faster to do this numerically.
Guffa
@Thorarin, Guffa: I have addressed both of your concerns, and avoided overflow issues.
Jason
It doesn't do any rounding however. Instead, it truncates to an integer. Your answer went from unreliable to plain wrong ;)
Thorarin
A: 
float myNum = 10.11;
char c = myNum[myNum.ToString().IndexOf(".") + 1];
Suroot
Does that round?
Byron Ross
That's truncation, it will always round down.
Babak Naffas
A: 

Get the fractional part, multiply by ten, and round:

double n = 345.38;
int digit = (int)Math.Round((n - Math.Floor(n)) * 10);

This avoids any overflow issues, as the result is already down to one digit when cast to an int.

I have verified that this gives the desired result for your examples.

Guffa
Breaks on negative numbers. Math.Truncate rather than Math.Floor would prevent that.
Thorarin
A: 

This whole overflow discussion is a little academic, and most likely not the intention of your homework. But should you want to solve that problem:

decimal value = -0.25m;
decimal fractionalPart = Math.Abs(value - Math.Truncate(value));
int digit = (int)Math.Round(10 * fractionalPart, MidpointRounding.AwayFromZero);

Edit: after reading your question again, I noticed that numbers shouldn't always be rounded up like my original answer. However, most people using Math.Round here use the default banker's rounding (to an even number). It depends if you intended -0.25 to result in 2 or 3. The way I'm reading your description, it should be 3 like in this example.

Thorarin
A: 

If you just want the digit immediately following the decimal...couldn't you do something like this?

float value; int digit = (int)(((value % 1) * 10) + 0.5)

Kunla
depending on the language, % can be an integer operation, so value % 1 will be zero. I actually can't think of a language where % is defined on floats, but that may be my own limitation. Wikipedia states that modular arithmetic is an integer operation, though.
mmr