0

on windows i want to know, how many digits will be after the decimal points using c++. I used locale.h header in windows, there is struct lconv , which gives a lot of information about locale setting but it gives the no of digits after decimal points only for monetary(money) quantities, but i have to get the locale setting for normal values.Please help me , how can i get the no of decimal digits after decimal point?

sanjay
  • 735
  • 1
  • 5
  • 23

2 Answers2

1

The amount of decimal characters in a number is not locale dependent. You can adjust the number of decimals displayed by a std::ostream by using std::setprecision. See the example there.

rubenvb
  • 74,642
  • 33
  • 187
  • 332
1

I misread the question the first time. My bad. Here's the amended answer.

This is not a "C++" answer, but since you said "on windows", I'll can cite to you how it can be done with Win32.

GetLocalInfoEx(lcid, LOCALE_IDIGITS, ...) for the number of digits after the decimal point.

#include <Windows.h>
#include <Winnls.h>
#include <stdio.h>

int GetNumberOfDigitsForDecimal()
{
    const int stringsize = 2; // docs for LOCALE_IDIGITS says this won't be more than 2 including the null char

    wchar_t value[stringsize] = {};
    GetLocaleInfoEx(LOCALE_NAME_USER_DEFAULT, LOCALE_NOUSEROVERRIDE|LOCALE_IDIGITS, value, stringsize);
    return _wtoi(value);
}

You didn't suggest how you wanted to obtain the locale (lcid) value, so I'm assuming for the current logged in user. Note, on Windows the user can go to the control panel and change his time, number, and date format specifiers. (Control Panel->Region->Additional Settings). If you want to respect his setting instead of that of the default for that language, you can remove the LOCALE_NOUSEROVERRIDE flag above.

If you need to support Windows XP, use GetLocaleInfo instead of GetLocaleInfoEx

selbie
  • 100,020
  • 15
  • 103
  • 173
  • There's also the LOCALE_SDECIMAL (max string size of 4) to get the actual character string that should be used to represent a decimal ("."). The docs suggest that it could be a comma in certain locales and cultures. – selbie Dec 31 '12 at 13:22