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
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.
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.
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.
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.
if (conto == (int)conto) {
}
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With