Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Formatting float to no decimals

Status
Not open for further replies.

glaing

Technical User
Oct 28, 2008
3
0
0
US
Hi,
I have bee trying to format the following field and cannot seem to get it right. All examples show consolewrite where as I am trying to put the formatted number in a cell. Here is the code

float l_perday = 0;
l_perday = (l_gros_renew + (l_gros_renew * l_ipt))/365;

I want l_perday to be formatted with comma's but no decimals.
Thank you
 
The other part of it, is that I am putting it into a grid so I want the cell to take the formatted value I.E

Grid_Result.Rows.Cells["Fin_Tot_Prem"].Value = l_gros_renew + (l_gros_renew * l_ipt);
l_perday = (l_gros_renew + (l_gros_renew * l_ipt))/365;
 
Not very sure what you're asking.

Are you from Europe where they write five thousand one hundred and twenty three point four as 5.123,4 or from UK/US where they write it as 5,123.4? Some people call the thousands separator a comma, regardless of whether it is a comma or a dot.

When you say you want commas, do yo mean you want 5.123 (European)/5,123(English)?
 
Hi,
The number is coming from the db as 7795.54. I want to remove the decimals and put in the seperator 7,795 rounding up to 7,796 would even be better but not absolutely necessary

Thanks
 
It's not an ideal solution but it works:
Code:
class Fmt {
public:
    Fmt(){}
    explicit Fmt(double x) {
        fmtnum(x);
    }
    Fmt& operator=(double x) {
        fmtnum(x);
        return *this;
    }
    const char* c_str() const { return s.c_str(); }
    const std::string& str() const { return s; }
private:
    std::string s;
    void trio(double x);
    void fmtnum(double x);
};

std::ostream& operator<<(std::ostream& os,const Fmt& f) 
{
    os << f.c_str();
    return os;
}

void Fmt::trio(double x)
{
    char buff[5];
    if (x < 1000.0)
        sprintf(buff,"%.0f",x);
    else {
        double y = fmod(x,1000.0);
        modf(x/1000.0,&x);
        trio(x);
        sprintf(buff,",%03.0f",y);
    }
    s += buff;
}

void Fmt::fmtnum(double x)
{
    s.erase();
    if (x < 0.0) {
        x = -x;
        s = "-";
    }
    trio(floor(x+0.5));
}
/*
    cout << Fmt(-7795.54) << endl;
    Fmt f;
    f = 12345678.0;
    cout << f << endl;
*/
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top