Hello ! I want to find the number of decimal places after the decimal point in the number.
For example: 2.715
Number returned should be 3 because there is only 3 decimal places after the decimal point.
Please help.
Hello ! I want to find the number of decimal places after the decimal point in the number.
For example: 2.715
Number returned should be 3 because there is only 3 decimal places after the decimal point.
Please help.
In what situation? Are you reading the number from a text string, or is it stored in a float type variable?
I want to find the number of digits after the decimal point and the data type is float. Like I am asking for a function?
A float value does not have a defined number of decimal digits. In fact, the decimal number 0.1 cannot be represented exactly using a float value.
Please explain what you really want to do. Start by telling us where you get that value.
Ok for your reference I take the value as a string from the serial monitor by using Serial.readString();
Taken as string and not float ( sorry )
One possibility is to use the c-string functions and character type functions to find the ASCII decimal point in the buffer, and count the number of ASCII decimal digits following it. For example, use strchr(), isdigit().
You will need to read the serial monitor data into a c-string (zero terminated character array) first.
See Serial Input Basics in the tutorials forum section.
Ok thanks!
jremington:
A float value does not have a defined number of decimal digits.
According to IEEE-754 standards, a float number has 23 digits after the decimal point of which only 5/6 are accurate.
GolamMostafa:
According to IEEE-754 standards, a float number has 23 binary digits after the decimal point, and represent only about 5/6 decimal digits.
Fixed that.
You're welcome.
(Strictly speaking, it's a radix point, not just a decimal point)
An Arduino float value can accurately represent somewhere between 6 and 7 decimal digits, depending on the actual value.
2-23 ~= 10-6.9
The reason I asked this question is because I was working on a serial calculator :-
String equation;
float sum;
void setup() {
 // put your setup code here, to run once:
 Serial.begin ( 9600 );
}
void loop() {
 // put your main code here, to run repeatedly:
 Serial.println ( "Enter equation: " );
 while ( !Serial.available() );
 equation = Serial.readString();
 sum = eval ( equation );
 Serial.println ( sum );
}
float eval ( String string )
{
 char arn[10];
 float a, n;
 int len;
 float sum, n1, n2;
 char op;
 float intVal;
 string.toCharArray ( arn, 50 );
 a = 1;
 n = 0;
 len = sizeof ( string );
 for ( int i = 0; i <= sizeof ( string ); i++ )
 {
  if ( arn[i] >= '0' && arn[i] <= '9' )
  {
   intVal = arn[i] - '0';
   n = n + ( intVal * a );
  }
  a = a / 10;
  if ( arn[i] == '+' || arn[i] == '-' || arn[i] == '*' || arn[i] == '/' )
  {
   op = arn[i];
   sum = n;
   n = 0;
   a = 1;
  }
 }
 switch ( op ) {
  case '+':
   sum = sum + n;
   break;
  case '-':
   sum = sum - n;
   break;
  case '*':
   sum = sum * n;
   break;
  case '/':
   sum = sum / n;
   break;
 }
 return sum;
}
Now please don't screw me for using the 'String' class but the answer I am getting in decimal ( answer is correct ). I want to make it into a proper integer type. Can someone help me? Thanks!
for ( int i = 0; i <= sizeof ( string )
Oops
although 32 bit IEEE 754 single precision Floating Point Numbert has 6/7 decimal digits of precision and a double 64 bit IEEE 754 double precision Floating Point Number has 14/15 decimal digits of precision if you are acquiring data with a precision of 2, 3 or 4 decimal digits, e.g. from a sensor, the results of float or double calculations will still have 2, 3 or 4 digits of precision or less. For example, subtraction of similar values can be a major cause of errors.
have look at The Deadly Consequences of Rounding Errors
TheMemberFormerlyKnownAsAWOL:
According to IEEE-754 standards, a float number has 23 binary digits after the decimal point, and represent only about 5/6 decimal digits.
You have appended the word "binary" before the word "digit" of my statement of Post#8. Did I forget to add it or I forgot to append the word "decimal"?
For example:
Say, I have all 1s for the 23 fractional bits of Fig-1.
Figure-1:
Every bit has its own positional weight and hence positional value. The last bit has the positional value of: 1/(2-23) 1 = 0.000000119209289550781251 = 0.00000011920928955078125 (23 decimal digits).
If we add all the positional values for all the fractional bits, we will get a fractional decimal number containing 23 decimal digits. So, will it be wrong to state like this: "According to IEEE-754 standards, a float number has 23 decimal digits after the decimal point of which only 6/7 digits are accurate?"
You can get 23 decimal digits out of 23 binary digits?
You should apply for the Fields Medal.
rising_youngstars:
Now please don't screw me for using the 'String' class but the answer I am getting in decimal ( answer is correct ). I want to make it into a proper integer type. Can someone help me? Thanks!
I think your question would be similar, with or without using the String class. The thing is, we can't read your mind. There is no such thing as a "proper integer type", there are only types. So you have to explain exactly what type you actually mean, and exactly what doesn't perform the you want it to do, and exactly what you do want to happen.
It would be extremely helpful if you could give concrete examples instead of just explaining it, as those are often clearer.
You can get 23 decimal digits out of 23 binary digits?
Easily
void setup() {
Serial.begin(9600);
float x=1.0/7.0;
Serial.println(x,23); //prints 0.14285714626312255859375
}
void loop() {}
Remarkably, the first 8 are correct!
Ha! How many will it print? Can I Serial.println(x,256) or something like that?
Edit - no but Serial.println(2.0/3, 255) produces
0.666666698455810546875000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
LOL
Except the answer is 0.142857 142857 142857 142857 142857...
Looks like I get to hang on to my fountain pen.