Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know if a float got a decimals

Tags:

ios

iphone

I'm building a simple calculator for a classroom, this is the code to show the result:

- (void)doTheMathForPlus:(float)val {
    float conto = self.contBox + val;
    self.myDisplay.text = [NSString stringWithFormat:@"%.02f", conto];
}

I need to know if "conto" have decimals (to change the format of the string in a if statement)

how to do it?

thanks

like image 952
Enlil Avatar asked Sep 07 '11 13:09

Enlil


People also ask

Can a float have decimals?

If doing math with floats, you need to add a decimal point, otherwise it will be treated as an int. See the Floating point constants page for details. The float data type has only 6-7 decimal digits of precision. That means the total number of digits, not the number to the right of the decimal point.

How do you know if it is a decimal or not?

function number_test(n) { var result = (n - Math. floor(n)) !== 0; if (result) return 'Number has a decimal place. '; else return 'It is a whole number.

Why is 0.1 not represented as a float?

The number 0.1 in floating-point The finite representation of 1/10 is 0.0 0011 ‾ 0.0\overline{0011} 0.00011, but it can't be represented in floating-point because we can't deal with bars in floating-point. We can represent it only in fixed digits/bits using any data type.

Where is the decimal point in a float expression?

For example, the number 4.1 × 103 is the decimal scientific notation for 4100. It has a mantissa of 4.1, a base of 10, and an exponent of 3. The decimal point floats to the position right after the most significant digit. Floating-point numbers are base 2 with a binary mantissa.


1 Answers

if (conto == (int)conto) {


}

Hope this helps.

like image 173
dredful Avatar answered Oct 03 '22 17:10

dredful