473,320 Members | 1,939 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,320 software developers and data experts.

read float from string and approximation problem

19
Hi,

I try to read a float in a string using sscanf or atof. It seems to work but then it's a bit strange when I manipulate the value obtained.

Expand|Select|Wrap|Line Numbers
  1.  
  2. float fl;
  3. char  str[]="1.43"
  4.  
  5. fl=atof(str);
  6.  
  7. printf("%f\n",floor(1000.0*fl) );
  8.  
  9.  
These lines return : 1429.000000

It seems to be due to the precision of the float type (Isn't it?).
Do you know how I can avoid that?

Regards
Mar 8 '08 #1
3 2614
MACKTEK
40
First I want to thankyou, I had not seen the floor() function before.

Perhaps you are looking for how to format your output?

Try changing your line as follows:
printf("%.2f\n",(floor(1000.0*fl)) );

The %.2f tells the printf you want 2 places to the right of the decimal.

There's a whole bunch more on how to format your output here.
Mar 9 '08 #3
Banfa
9,065 Expert Mod 8TB
These lines return : 1429.000000

It seems to be due to the precision of the float type (Isn't it?).
Do you know how I can avoid that?
Avoid what? you have not actually stated what you find undesirable about this output, the code is working correctly.
Mar 9 '08 #4

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

Similar topics

6
by: Bengt Richter | last post by:
Peculiar boundary cases: >>> 2.0**31-1.0 2147483647.0 >>> int(2147483647.0) 2147483647L >>> int(2147483647L ) 2147483647 >>> >>> -2.0**31
8
by: Rene | last post by:
The following code: float someFloat = 0; for(int x = 1; x <= 10; x++) { someFloat += .1F; Console.WriteLine(someFloat.ToString()); }
8
by: a | last post by:
I have a struct to write to a file struct _structA{ long x; int y; float z; } struct _structA A; //file open write(fd,A,sizeof(_structA)); //file close
60
by: Erick-> | last post by:
hi all... I've readed some lines about the difference between float and double data types... but, in the real world, which is the best? when should we use float or double?? thanks Erick
27
by: galt_57 | last post by:
I need to do just a few multiplies of long integers and have a divide by ten. Can I avoid using floats? Can I use two longs as a 64 bit value somehow? Thanks.
116
by: Dilip | last post by:
Recently in our code, I ran into a situation where were stuffing a float inside a double. The precision was extended automatically because of that. To make a long story short, this caused...
23
by: JDT | last post by:
Hi, It seems that using floats as the first tupelo for an STL map (shown below) can cause problems but I don't remember what the problems were. Is it common practice to use a structure like...
3
by: Nader | last post by:
Hello, I have a list of tuple with strin elements. These elements are number, but they are save as string. Now I will change the string to number which will be rounded. An example will make it...
63
by: Bill Cunningham | last post by:
I don't think I can do this without some help or hints. Here is the code I have. #include <stdio.h> #include <stdlib.h> double input(double input) { int count=0,div=0; double...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.