Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 12th, 2008, 9:54 AM   #1
maligree
Newbie
 
Join Date: Oct 2007
Posts: 4
Rep Power: 0 maligree is on a distinguished road
Sum of digits in a float?

Hey everyone,

Simple - what's a nice way to count the sum of digits in a floating point number (e.g. 176.3)? Is there a function similar to atoi and itoa but for floats?

EDIT: Actually, I'm more interested in converting float to ASCII, summing up digits is the next step.

Thanks in advance!

Last edited by maligree; Jul 12th, 2008 at 10:10 AM.
maligree is offline   Reply With Quote
Old Jul 12th, 2008, 10:23 AM   #2
Klarre
Game engine designer
 
Klarre's Avatar
 
Join Date: May 2005
Location: Sweden
Posts: 310
Rep Power: 4 Klarre is on a distinguished road
Re: Sum of digits in a float?

One way to convert a float to a string is to use std::stringstream.
Something like this:
float x = 1.0f;
std::stringstream theString;
theString << x;
const char* result = theString.str().c_str();

EDIT: Oops, this was in the C forum not in C++. std::stringstream is only available in C++. Sorry...
__________________
http://www.klarre.se
Klarre is offline   Reply With Quote
Old Jul 12th, 2008, 1:44 PM   #3
Seif
Hobbyist Programmer
 
Seif's Avatar
 
Join Date: Jan 2006
Location: UK
Posts: 225
Rep Power: 3 Seif is on a distinguished road
Re: Sum of digits in a float?

sprintf

http://www.cplusplus.com/reference/c...o/sprintf.html
Seif is offline   Reply With Quote
Old Jul 12th, 2008, 4:37 PM   #4
maligree
Newbie
 
Join Date: Oct 2007
Posts: 4
Rep Power: 0 maligree is on a distinguished road
Re: Sum of digits in a float?

Ah, thanks a lot.. it seems painfully obvious now.
maligree is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting 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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Slight Float Problem. Code and Screenshot. brokenhope HTML / XHTML / CSS 0 Nov 27th, 2007 5:31 PM
counting digits rwm C++ 25 Apr 11th, 2007 8:16 AM
set a number to 2 digits only hipolito C 24 Jul 17th, 2006 4:47 PM
Two ints divided into a float. Dygear C++ 2 Apr 1st, 2005 6:42 PM
float precision and MFC... Racine_ C++ 0 Feb 5th, 2005 9:31 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:45 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC