Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > How to trim a number for a certain amount of significant figures?

How to trim a number for a certain amount of significant figures?
Thread Tools
Junior Member
Join Date: Jan 2002
Status: Offline
Reply With Quote
Jun 28, 2003, 01:34 PM
 
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
Reply With Quote
Jun 28, 2003, 02:01 PM
 
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
Reply With Quote
Jun 28, 2003, 09:08 PM
 
Sorry!

C, plain, vanilla, ordinary, K&R type.
     
Mac Elite
Join Date: Dec 2001
Location: Atlanta, GA, USA
Status: Offline
Reply With Quote
Jun 28, 2003, 09:46 PM
 
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
Reply With Quote
Jun 28, 2003, 10:24 PM
 
... 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
Reply With Quote
Jun 28, 2003, 10:50 PM
 
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
Reply With Quote
Jul 1, 2003, 03:46 PM
 
Thanks for the suggestion. I'll work on it using your thoughts.
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 03:45 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2