LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   count digits of a float || convert float to string (http://www.linuxquestions.org/questions/programming-9/count-digits-of-a-float-%7C%7C-convert-float-to-string-342216/)

nadroj 07-11-2005 02:51 PM

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 :confused: ), 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.

jonaskoelker 07-11-2005 03:18 PM

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

deiussum 07-11-2005 03:18 PM

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


nadroj 07-11-2005 03:47 PM

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.

nadroj 07-11-2005 03:57 PM

found it :D
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!

deiussum 07-11-2005 04:18 PM

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... ;)

nadroj 07-11-2005 04:52 PM

Yes.. the java example and comparison to the C++ method did help! Thanks for taking the time and explaning it


All times are GMT -5. The time now is 09:41 PM.