 |
 |
How to trim a number for a certain amount of significant figures?
|
 |
|
 |
|
Junior Member
Join Date: Jan 2002
Status:
Offline
|
|
Basically, I have a program that deals with a lot of double data type numbers with a variable number of significant figures.
I want to present my user with only the significant figures of a number, so if 6 sig figs were allowed, a number such as 23.4563456 would become 23.4563.
Does anyone know if there is a library function that would allow me to input any number, the number of sig figs, and output the correct value, or will I have to hand-code this?
Thanks!
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Dec 2001
Location: Atlanta, GA, USA
Status:
Offline
|
|
Originally posted by Propofol:
Basically, I have a program that deals with a lot of double data type numbers with a variable number of significant figures.
I want to present my user with only the significant figures of a number, so if 6 sig figs were allowed, a number such as 23.4563456 would become 23.4563.
Does anyone know if there is a library function that would allow me to input any number, the number of sig figs, and output the correct value, or will I have to hand-code this?
Thanks!
What programming language?
|
|
Mac Pro 2x 2.66 GHz Dual core, Apple TV 160GB, two Windows XP PCs
|
| |
|
|
|
 |
|
 |
|
Junior Member
Join Date: Jan 2002
Status:
Offline
|
|
Sorry!
C, plain, vanilla, ordinary, K&R type.
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Dec 2001
Location: Atlanta, GA, USA
Status:
Offline
|
|
Originally posted by Propofol:
Sorry! 
C, plain, vanilla, ordinary, K&R type.
Do you want to display the number in rounded form, or actually round the number?
|
|
Mac Pro 2x 2.66 GHz Dual core, Apple TV 160GB, two Windows XP PCs
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Jan 2001
Status:
Offline
|
|
... printf() can do this quite easily. let's say you have a float f = 2.33445566. if you do:
printf("%0.2f\n",f);
it will print: 2.33
check out the sprintf variants if you want to store the remaining string.
type:
man printf
into the terminal to see all the nifty things it can do
|
|
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: Jun 1999
Location: Las Vegas, NV, USA
Status:
Offline
|
|
Couldn't you find a routine to convert it to scientific notation (2.34563456E+1). Then take the mantissa and work on it to get just the number of digits you want (2.34563E+1) and convert the result back to decimal?
|
|
|
| |
|
|
|
 |
|
 |
|
Junior Member
Join Date: Jan 2002
Status:
Offline
|
|
Thanks for the suggestion. I'll work on it using your thoughts.
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|