2

When I write a simple wprintf function and pass a double to it a float number using the verb notation L"%.2f" it simply prints an "f" on the screen instead of a number like in the format 0.00.

I would like some help as everywhere I look it simply says L"%.2f" is the way to print a number with 2 decimal digit precision.

float duration = (float)((std::clock() - mTimer) / (float)(CLOCKS_PER_SEC / 1000));
wsprintf(message, L">> Speed: %.2f\r\n", duration);

These are the 2 lines causing my headache... they result in

>> Speed: f

being printed on the console.

The output i'm looking for is this:

>> Speed: 4000.00
3
  • What is wsprintf()? There's a swprintf() in the standard, but it takes different arguments... Commented Apr 17, 2019 at 12:23
  • Ah, think I found it. Some Windows thing? Pay close attention to the documentation if so. Commented Apr 17, 2019 at 12:27
  • Thank you i will also research this as i went with the swprintf() option in the end. But i will look in to the MSDN info you provided, i only code as a hobby so i'm not really trained or a professional lol. All the information i can get it much appreciated. Thank you Shawn Commented Apr 18, 2019 at 9:16

2 Answers 2

5

The function wsprintf() (which seems to be windows specific) does not support floating point parameters, You can go for swprintf() (which is a standard library function) instead.

Moreover, there is a note in the wsprintf documentation which states:

Do not use. Consider using one of the following functions instead: StringCbPrintf, StringCbPrintfEx, StringCchPrintf, or StringCchPrintfEx. See Security Considerations.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for this update, ill look at everything you said and decide on which i feel best thank you
I have changed over to use swprintf() it makes a lot sense thanks very much P.W my code works as expected :)
0

What you can do is :

cout << fixed << setprecision(2) << message << ">> Speed : "<< duration << endl;

2 Comments

Writing a logger, it's interface is generic pure virtual class but it's implementation it's platform specific. I'm writing the windows part now so i'm sticking to using all of the windows stuff and taking advantage of what Microsoft made specifically for their operating system but you might be right, using a stream might be the easier option
Oh my bad, I did not figure out that you were trying to make a logger.... Should have paid more attention, my excuses .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.