Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Need to get a float value into a wchar_t array...

SephirothSephiroth Fayetteville, NC, USAPosts: 1,035Member
Alright, I am creating a high-speed, lightweight, object-oriented windowing library in C++ and am working on a control that looks and acts like the "NumericUpDown" in VB.NET. I have most of it up, but have run into a problem. I store the value in a float variable, but you cannot directly plop a float value into an edit control. The thing is, the user is allowed to specify three types of display. One is currency, which will display a dollar sign and the value including two decimal-places. The other is a straight integer-style which is just the number without any decimal places, and the final is a float value with one to eight decimal places.

That said, how could I go about putting the float value into an array of wchar_t type variables without looping and what-not? I know the old ANSI method of using sprintf(), but since this is a UNICODE, C++ project, I thought that maybe there would be a more efficient way of doing things. Thanks for the help!

*EDIT*

Here is what I am thinking, but am not sure whether or not it will work. The "bCurrency" flag indicates whether or not the value being stored is money.
[code]
//Format the number properly
if(this->bCurrency)
wsprintf(pFormat, L"$%8.2f");
else
wsprintf(pFormat, L"%8.%df", this->Decimals);

//Now generate the actual display buffer
wsprintf(pBuffer, pFormat, this->fValue);
[/code]
Is that doable, or a bad practice? The buffer is wchar_t[64] and the format is wchar_t[8].

-[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]

Comments

  • rageshctechrageshctech Posts: 20Member
    wspribtf is the best option i think.
  • AsmGuru62AsmGuru62 Posts: 6,519Member
    [color=Blue]
    When describing wsprintf() - MSDN does not mention any formats for float point values, however, '.precision' is specified as one of components in the format - very puzzling... You should check if this will really work.
    [/color]
  • AsmGuru62AsmGuru62 Posts: 6,519Member
    This post has been deleted.
  • SephirothSephiroth Fayetteville, NC, USAPosts: 1,035Member
    Well I was hoping to do this in a C++ way. Isn't there some way to do it using stuff like ""? I am only familiar with the older *sprintf functions due to my long history with C, dating back to DOS on a 286! I'd like to drop those calls if possible. So can this be done in C++ itself without using the *sprintf functions? My destination buffer is wchar_t, of course.

    *EDIT*

    Would something like this work?
    [code]
    //Format the number properly
    if(this->bCurrency)
    swprintf(pBuffer, 64, L"$%10.2f", this->fValue);
    else
    {
    swprintf(pFormat, 8, L"%10.%if", this->Decimals);
    swprintf(pBuffer, 64, pFormat, this->fValue);
    }
    [/code]
    -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]
Sign In or Register to comment.