473,386 Members | 1,883 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How to parse float number

Hi, my name is Amarendra..
I am building a parser for CAD translator.. My parser right now can parse integer numbers easily. But it has a problem while reading float number. Could anybode please help me.
The code is as follows.

Expand|Select|Wrap|Line Numbers
  1. if (isdigit(ch))
  2.             {
  3.                 int num=0;
  4.                 do
  5.                 {
  6.                     //no checking for overflow
  7.                     num=10 * num + ch - '0';
  8.                 }while((ch = read_ch())!=EOF && isalnum (ch));
  9.                 put_back(ch);
  10.                 return num;
  11.             }
  12. }
  13.  
Nov 3 '06 #1
2 16526
horace1
1,510 Expert 1GB
It depends how sophisticated you want to be. You could
1.loop reading the whole number part – exit on a .
2.loop reading the fractional part exit on a non digit

For example the following assumes the first non digit is a . so you would want to add error checking, etc
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5.     float num=0, decimal, divisor=10;;
  6.     char ch;
  7.     while(1)
  8.          if(isdigit(ch=getche())) num=10 * num + ch - '0';
  9.          else break;                   // assume a . here
  10.     while(1)
  11.           if(isdigit(ch=getche())) {num=num + (ch - '0')/divisor; divisor *= 10; }
  12.           else break;                   // non digit
  13.     printf("num = %f\n", num);
  14. }
  15.  
Nov 3 '06 #2
horace1
1,510 Expert 1GB
Simpler version of previous code would be
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <ctype.h>
  3. int main()
  4. {
  5.     float num=0, decimal, divisor=10;;
  6.     char ch;
  7.     while(isdigit(ch=getchar())) num=10 * num + ch - '0';
  8.     // assume a . here
  9.     while(isdigit(ch=getchar())) {num=num + (ch - '0')/divisor; divisor *= 10; }
  10.     // non digit
  11.     printf("num = %f\n", num);
  12. }
  13.  
or in C you could use sscanf(), e.g.
Expand|Select|Wrap|Line Numbers
  1. // parse a decimal number
  2. #include <stdio.h>
  3. #include <ctype.h>
  4. int main()
  5. {
  6.     char data[]=" 3.14159";
  7.     float fnum=0;
  8.     // attempt to read a float from data[]
  9.     if(sscanf(data, "%f", &fnum)) printf("float = %f\n",fnum);
  10.     else                          printf("input fail\n");
  11. }
  12.  
program output would be
float = 3.141590


or in C++ you could use strstream, e.g.
Expand|Select|Wrap|Line Numbers
  1. // parse a decimal number
  2. #include <iostream>
  3. #include <strstream>
  4. using namespace std; 
  5.  
  6. int main()
  7. {
  8.     char data[20]=" 3.1415926   2.718";
  9.     float fnum=0;
  10.     // attempt to read a float from data[]
  11.     istrstream str(data,20);
  12.     str >> fnum;
  13.     if(str.fail())  { cout << "input fail\n"; exit(1); } 
  14.     cout << "float = " << fnum << endl;
  15.     str >> fnum;
  16.     if(str.fail())  { cout << "input fail\n"; exit(1); } 
  17.     cout << "float = " << fnum << endl;
  18. }
  19.  
program output would be
float = 3.14159
float = 2.718
Nov 4 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: respect | last post by:
i want to convert real float number to 32-bit IEEE binary format in C++ ? plz help me in finding the codes as fast as u can ] Example for input -6.5, the output should be. 1 10000001...
7
by: A. L. | last post by:
Consider following code segment: #1: double pi = 3.141592653589; #2: printf("%lf\n", pi); #3: printf("%1.12lf\n", pi); #4: printf("%1.15lf\n", pi); The above code outputs as following: ...
3
by: yaron | last post by:
Hi all, i need to read a float number in my c# client that is received from a socket from a java server. 1. i think i found a sollution, here it is : public float readFloat() { return...
9
by: Python.LeoJay | last post by:
Dear all, i need to parse billions of numbers from a file into float numbers for further calculation. i'm not satisfied with the speed of atof() function on my machine(i'm using visual c++ 6)....
10
by: dkk | last post by:
Here is the code: #include <stdio.h> #include <stdlib.h> int main(void) { float f; int part1, part4; float part2; char part3;
43
by: Xancatal | last post by:
Hey everybody. I need help on this one. I need to verify that a number entered by a user is not either a negative number (-100.00), or an alphabet (a, b, c, X, Y) as well as other number other than...
6
by: Toe001 | last post by:
In a program for use with a micro, I have a variable of type float (real number) and wish to save it to EEPROM. I have a routine which writes a byte at a time to EEPROM at the location (address) I...
0
by: Timothy Grant | last post by:
That's because s IS a string. It's not been converted to a float. In : s = '3.1415' In : n = float(s) In : type(s) Out: <type 'str'> In : type(n) Out: <type 'float'> Why are you avoiding...
7
by: Udara chathuranga | last post by:
How can I convert binary float number into a decimal float number ?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.