LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-11-2005, 02:51 PM   #1
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 59
count digits of a float || convert float to string


im new to c++ and working on a console app.
i still cant grasp pointers and 'char *'s fully.. so im using cstring header file and using 'string's (however devC++ doesnt bold it like other data types ), but thats a whole other story.

i have a function that returns a float value and prints it in a table i made. but if the value is, say 1.2 and another value is 11.5 then the tables alignment gets all screwed up. i looked briefly for a padding type of function so the table's columns would always line up, without success.

so i thought of a different method, was to either convert the float to a string or make a string with the floats value in it, count the length of that string, then i could do the rest (insert spaces as need be, to be sure all cells are X characters long). sounded like a good idea to me, but i dont know how to convert float -> string. and i dont even know how to concatenate a string with another value. i figured it was simple like java but i cant even figure it out.

iv checked afew sites and cant find out how to convert float to string. how come i can find so many functions that convert char to int,double,float etc.. but cant do the other way around?

any help appreciated, thanks.

Last edited by nadroj; 07-11-2005 at 02:57 PM.
 
Old 07-11-2005, 03:18 PM   #2
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 46
If you're using C-style char*, have a look at `sprintf'; if you're using C++ strings (instances of the class `string'), have a look at the strstream header--the stringstreams have a toString-like method. GIYF.

hth --Jonas
 
Old 07-11-2005, 03:18 PM   #3
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 31
Here's an example that might help you. Shows how to use a fill character and width to align your numbers, and shows how you convert a float (or pretty much any other numeric type) to a string using a stringstream (kind of the C++ equivalent to the Java StringBuilder class).

As a side note, the setw and setfill manipulators work with cout and fstreams as well so if you are already using one of those, you don't really need to use the stringstream to convert to a string first.

Code:
#include <iostream> // For cout
#include <sstream>  // For stringstream
#include <iomanip>  // for setw and setfill

int main()
{
    std::stringstream ss;
    float f1 = 1.2;
    float f2 = 11.5;

    ss << std::setw(4) <<       // Set minimum width of output
        std::setfill(' ') <<    // Set fill character to satisfy min output
        f1 << std::endl <<
        std::setw(4) <<
        std::setfill(' ') <<
        f2 << std::endl;

    std::cout << ss.str();

}
Code:
Output:
 1.2
11.5

Last edited by deiussum; 07-11-2005 at 03:27 PM.
 
Old 07-11-2005, 03:47 PM   #4
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Original Poster
Rep: Reputation: 59
ya, as mentioned i am using c++ strings.
if i have the 'using namespace std;' does this allow my to avoid putting that 'std::' stuff you had? cause thats how i have it now (without the std: and it works.. i just dont know what std:: means and, as i said, am new to c++ so i dont wanna just use what i find and call it my own program, but rather work my way up to that and using it after finding out what it is and how to use it, etc.

is this the only way to do this sort of padding? i used your suggestion and it does work good.. however, is there a way to make the output left justified? so that all the cells' data start on left.. so like padright?

thanks for the info though guys.
 
Old 07-11-2005, 03:57 PM   #5
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Original Poster
Rep: Reputation: 59
found it
Code:
ss << setw(5) << setfill(' ') << setiosflags (ios_base::left) << h;
return ss.str();
just wish there was a more straightforward/less cryptic way to do this.. oh well, thanks!
 
Old 07-11-2005, 04:18 PM   #6
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 31
No problem.

EDIT: Going to try and re-word my description of the using namespace syntax to make it a bit more clear.... Since you say you've used Java, hopefully the comparison to Java is helpful.

For this example, let's say you have a class called Dude, in a namespace called Sweet. In Java, you could do one of 2 things:

Code:
import Sweet.*;

public class SomeClass
{
    public static void main(String[] args)
   {
        Dude d = new Dude();
        // use d
   }
}

Or:

public class SomeClass
{
    public static void main(String[] args)
   {
        Sweet.Dude d = new Sweet.Dude();
        // use d
   }
}
In C++ you also have 2 ways of doing the same thing above. That would look like so:
Code:
#include "Dude.h"

using namespace Sweet;

int main()
{
    Dude *d = new Dude;  // Or just Dude d; to put it on the stack

    // use d
    return 0;
}

Or:

#include "Dude.h"

int main()
{
    Sweet::Dude *d = new Sweet::Dude;  // Or just Sweet::Dude d; to put it on the stack

    // use d
    return 0;
}
Hope that makes more sense.

Disclaimer: I haven't worked with Java much in years so take my Java example with a grain of salt... ;)

Last edited by deiussum; 07-11-2005 at 04:39 PM.
 
Old 07-11-2005, 04:52 PM   #7
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Original Poster
Rep: Reputation: 59
Yes.. the java example and comparison to the C++ method did help! Thanks for taking the time and explaning it
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to convert a float to its binary or hex representation in Python? zero79 Programming 1 09-01-2005 10:19 AM
How to convert a float to its binary or hex representation in Python? zero79 Linux - Software 1 08-29-2005 09:30 PM
how big is a float in C? SciYro Programming 11 04-04-2005 09:24 AM
can't convert o to FLoat in Java alaios Programming 4 04-24-2004 02:10 PM
Float/Double to String? Mega Man X Programming 16 01-03-2004 07:50 PM


All times are GMT -5. The time now is 02:06 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration