TSTRING OverwriteDlg::_GetNumberFormat(UINT64 ui64Num) { TSTRING r; sprintf(r, _T("%I64d"), ui64Num); int iAlloc = GetNumberFormat(LOCALE_SYSTEM_DEFAULT, 0UL, r.c_str(), NULL, NULL, 0); if (0 < iAlloc) { TCHAR *szBuf = new TCHAR[iAlloc]; if (NULL != szBuf) { RtlZeroMemory(szBuf, iAlloc * sizeof(TCHAR)); NUMBERFMT nf = {0}; TSTRING tsDecSep = _GetLocaleInfoString(LOCALE_SMONDECIMALSEP); TSTRING tsThouSep = _GetLocaleInfoString(LOCALE_STHOUSAND); nf.LeadingZero = _GetLocaleInfoNumeric(LOCALE_ILZERO); nf.Grouping = _GetLocaleInfoNumeric(LOCALE_SGROUPING); nf.lpDecimalSep = const_cast<TCHAR *>(tsDecSep.c_str()); nf.lpThousandSep = const_cast<TCHAR *>(tsThouSep.c_str()); nf.NegativeOrder = _GetLocaleInfoNumeric(LOCALE_INEGNUMBER); nf.NumDigits = 0U; if (0 != GetNumberFormat(LOCALE_SYSTEM_DEFAULT, 0UL, r.c_str(), &nf, szBuf, iAlloc)) { r = szBuf; } delete szBuf; } } return r; }
CString FormatNumber(CString szNumber, bool bNoFractionalDigits /*= true*/) { CString ret; int nChars = GetNumberFormat(LOCALE_USER_DEFAULT, 0, szNumber, nullptr, nullptr, 0); GetNumberFormat(LOCALE_USER_DEFAULT, 0, szNumber, nullptr, ret.GetBuffer(nChars), nChars); ret.ReleaseBuffer(); if (bNoFractionalDigits) { TCHAR szNumberFractionalDigits[2] = {0}; GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_IDIGITS, szNumberFractionalDigits, _countof(szNumberFractionalDigits)); int nNumberFractionalDigits = _tcstol(szNumberFractionalDigits, nullptr, 10); if (nNumberFractionalDigits) { ret.Truncate(ret.GetLength() - nNumberFractionalDigits - 1); } } return ret; }
String FormatNum(__int64 n) { static bool first = true; static NUMBERFMT fmt; static wchar_t DecimalSep[4]; static wchar_t ThousandSep[4]; if (first) { GetLocaleInfo(LOCALE_USER_DEFAULT,LOCALE_STHOUSAND,ThousandSep,ARRAYSIZE(ThousandSep)); GetLocaleInfo(LOCALE_USER_DEFAULT,LOCALE_SDECIMAL,DecimalSep,ARRAYSIZE(DecimalSep)); DecimalSep[1]=0; // âèíäå ñåïàðàòîðû öèôð ìîãóò áûòü áîëüøå îäíîãî ñèìâîëà ThousandSep[1]=0; //íî äëÿ íàñ ýòî áóäåò íå î÷åíü õîðîøî /* if (LOWORD(Global->Opt->FormatNumberSeparators)) { *DecimalSep=LOWORD(Global->Opt->FormatNumberSeparators); } if (HIWORD(Global->Opt->FormatNumberSeparators)) { *ThousandSep=HIWORD(Global->Opt->FormatNumberSeparators); } */ fmt.LeadingZero = 1; fmt.Grouping = 3; fmt.lpDecimalSep = DecimalSep; fmt.lpThousandSep = ThousandSep; fmt.NegativeOrder = 1; fmt.NumDigits = 0; first = false; } String strSrc(n); int size = GetNumberFormat(LOCALE_USER_DEFAULT, 0, strSrc.c_str(), &fmt, nullptr, 0); wchar_t *lpwszDest = new wchar_t[size]; GetNumberFormat(LOCALE_USER_DEFAULT, 0, strSrc.c_str(), &fmt, lpwszDest, size); String strDest(lpwszDest); delete(lpwszDest); return strDest; }
bool CFileBrowserListCtrl::FormatSize(ULONGLONG Size, CString& Str) { static const NUMBERFMT NumFmt = {0, 0, 3, _T("."), _T(","), 0}; // no decimal places static const int MAX_STRING = 64; // maximum number string TCHAR ValStr[MAX_STRING], CommaStr[MAX_STRING]; _stprintf(ValStr, _T("%I64d"), (Size >> 10) + ((Size & 0x3ff) != 0)); // to KB, rounding up if (!GetNumberFormat(0, 0, ValStr, &NumFmt, CommaStr, MAX_STRING)) return(FALSE); Str = CString(CommaStr) + " KB"; return(TRUE); }
String NumberToString(Number num) { char buff[32]; if (-2147483647. <= num && num < 2147483648. && static_cast<Number>(static_cast<int>(num)) == num) { ::sprintf(buff, "%d", static_cast<int>(num)); } else if (0 <= num && num <= 4294967295. && static_cast<Number>(static_cast<UInt>(num)) == num) { ::sprintf(buff, "%u", static_cast<UInt>(num)); } else { ::sprintf(buff, GetNumberFormat(), num); } return String(buff); }
string &FormatNumber(const wchar_t *Src, string &strDest, int NumDigits) { static bool first = true; static NUMBERFMT fmt; static wchar_t DecimalSep[4]; static wchar_t ThousandSep[4]; if (first) { GetLocaleInfo(LOCALE_USER_DEFAULT,LOCALE_STHOUSAND,ThousandSep,ARRAYSIZE(ThousandSep)); GetLocaleInfo(LOCALE_USER_DEFAULT,LOCALE_SDECIMAL,DecimalSep,ARRAYSIZE(DecimalSep)); DecimalSep[1]=0; //В винде сепараторы цифр могут быть больше одного символа ThousandSep[1]=0; //но для нас это будет не очень хорошо if (LOWORD(Opt.FormatNumberSeparators)) *DecimalSep=LOWORD(Opt.FormatNumberSeparators); if (HIWORD(Opt.FormatNumberSeparators)) *ThousandSep=HIWORD(Opt.FormatNumberSeparators); fmt.LeadingZero = 1; fmt.Grouping = 3; fmt.lpDecimalSep = DecimalSep; fmt.lpThousandSep = ThousandSep; fmt.NegativeOrder = 1; first = false; } fmt.NumDigits = NumDigits; string strSrc=Src; int Size=GetNumberFormat(LOCALE_USER_DEFAULT,0,strSrc,&fmt,nullptr,0); wchar_t* lpwszDest=strDest.GetBuffer(Size); GetNumberFormat(LOCALE_USER_DEFAULT,0,strSrc,&fmt,lpwszDest,Size); strDest.ReleaseBuffer(); return strDest; }
// This function accepts a number and converts it to a // string, inserting commas where appropriate. PTSTR BigNumToString(LONG lNum, PTSTR szBuf, DWORD chBufSize) { TCHAR szNum[100]; StringCchPrintf(szNum, _countof(szNum), TEXT("%d"), lNum); NUMBERFMT nf; nf.NumDigits = 0; nf.LeadingZero = FALSE; nf.Grouping = 3; nf.lpDecimalSep = TEXT("."); nf.lpThousandSep = TEXT(","); nf.NegativeOrder = 0; GetNumberFormat(LOCALE_USER_DEFAULT, 0, szNum, &nf, szBuf, chBufSize); return(szBuf); }
//*************************************************************************************** void CBCGPShellList::OnFormatFileSize (long lFileSize, CString& str) { str.Empty (); if (lFileSize == 0) { str = _T("0"); } else { lFileSize = lFileSize / 1024 + 1; str.Format (_T("%ld"), lFileSize); //------------------------------------- // Convert number to the system format: //------------------------------------- TCHAR szNumOut [256]; GetNumberFormat (LOCALE_USER_DEFAULT, LOCALE_NOUSEROVERRIDE, str, NULL, szNumOut, 255); str = szNumOut; //---------------------------------- // Truncate trailing fractal digits: //---------------------------------- TCHAR szDec [10]; GetLocaleInfo (LOCALE_USER_DEFAULT, LOCALE_SDECIMAL, szDec, 10); int nDecLen = lstrlen (szDec); if (nDecLen > 0) { for (int i = str.GetLength () - nDecLen - 1; i >= 0; i--) { if (str.Mid (i, nDecLen) == szDec) { str = str.Left (i); break; } } } } str += _T(" KB"); }
void CMd5ItemCached::SetMd5Item(CMd5Item *pMd5Item) { if ((pMd5Item != m_pMd5Item) || (pMd5Item->GetVersion() != m_nVersion)) { m_pMd5Item = pMd5Item; m_nVersion = pMd5Item->GetVersion(); if (pMd5Item->GetFileName() == pMd5Item->GetFullPath()) *m_szFolderPath = 0; else { _tcsncpy_s(m_szFolderPath, MAX_PATH, pMd5Item->GetFullPath(), _TRUNCATE); PathRemoveFileSpec(m_szFolderPath); } if (!pMd5Item->IsNA()) { TCHAR szSize[MAX_NUM_STR]; NUMBERFMT nf; nf.NumDigits = 0; nf.LeadingZero = 0; nf.Grouping = 3; nf.lpDecimalSep = _T("."); nf.lpThousandSep = _T(","); nf.NegativeOrder = 1; _stprintf_s(szSize, MAX_NUM_STR, _T("%lld"), pMd5Item->GetSize()); GetNumberFormat(LOCALE_USER_DEFAULT, 0, szSize, &nf, m_szSize, MAX_NUM_STR); TCHAR szDate[MAX_DATE_STR]; TCHAR szTime[MAX_DATE_STR]; SYSTEMTIME st; FileTimeToSystemTime(&pMd5Item->GetModified(), &st); GetDateFormat(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &st, NULL, szDate, MAX_DATE_STR); GetTimeFormat(LOCALE_USER_DEFAULT, 0, &st, NULL, szTime, MAX_DATE_STR); _stprintf_s(m_szModified, MAX_DATE_STR, _T("%s %s"), szDate, szTime); } else { *m_szSize = 0; *m_szModified = 0; } } }
void BlobMgmt::ShowBlobSize(wyInt32 datasize) { wyString strnumber ; //XFormat NUMBERFMT nf; wyInt32 nsize; wyWChar *formattednumber; wyString str; strnumber.Sprintf("%d", datasize); memset(&nf, 0, sizeof(nf)); nf.lpThousandSep = L","; nf.lpDecimalSep = L"."; nf.LeadingZero = 1; nf.Grouping = 3; nf.NegativeOrder = 1; //string length + noof commas + 1 nsize = strnumber.GetLength() + strnumber.GetLength()/3 + 1; formattednumber = AllocateBuffWChar(nsize); if (GetNumberFormat(LOCALE_USER_DEFAULT, 0, (LPCWSTR)strnumber.GetAsWideChar(), &nf, formattednumber, nsize)) { strnumber.SetAs(formattednumber); } //adding "bytes" strnumber.AddSprintf(" bytes"); SendMessage(GetDlgItem(m_hwnddlg, IDC_BLOBSIZE ), WM_SETTEXT, strnumber.GetLength(), (LPARAM) strnumber.GetAsWideChar()); if(formattednumber) free(formattednumber); }
/* Update all locale samples */ static VOID UpdateLocaleSample(HWND hwndDlg, LCID lcidLocale) { TCHAR OutBuffer[MAX_SAMPLES_STR_SIZE]; /* Get number format sample */ GetNumberFormat(lcidLocale, NO_FLAG, SAMPLE_NUMBER, NULL, OutBuffer, MAX_SAMPLES_STR_SIZE); SendMessage(GetDlgItem(hwndDlg, IDC_NUMSAMPLE_EDIT), WM_SETTEXT, 0, (LPARAM)OutBuffer); /* Get monetary format sample */ GetCurrencyFormat(lcidLocale, LOCALE_USE_CP_ACP, SAMPLE_NUMBER, NULL, OutBuffer, MAX_SAMPLES_STR_SIZE); SendMessage(GetDlgItem(hwndDlg, IDC_MONEYSAMPLE_EDIT), WM_SETTEXT, 0, (LPARAM)OutBuffer); /* Get time format sample */ GetTimeFormat(lcidLocale, NO_FLAG, NULL, NULL, OutBuffer, MAX_SAMPLES_STR_SIZE); SendMessage(GetDlgItem(hwndDlg, IDC_TIMESAMPLE_EDIT), WM_SETTEXT, 0, (LPARAM)OutBuffer); /* Get short date format sample */ GetDateFormat(lcidLocale, DATE_SHORTDATE, NULL, NULL, OutBuffer, MAX_SAMPLES_STR_SIZE); SendMessage(GetDlgItem(hwndDlg, IDC_SHORTTIMESAMPLE_EDIT), WM_SETTEXT, 0, (LPARAM)OutBuffer); /* Get long date sample */ GetDateFormat(lcidLocale, DATE_LONGDATE, NULL, NULL, OutBuffer, MAX_SAMPLES_STR_SIZE); SendMessage(GetDlgItem(hwndDlg, IDC_FULLTIMESAMPLE_EDIT), WM_SETTEXT, 0, (LPARAM)OutBuffer); }
/////////////////////////////////////////////////////////////////////////////////////////// // Function: InitNLSFields // // Description: Initialize NLS formatting fields for a given LCID // // Comments: // /////////////////////////////////////////////////////////////////////////////////////////// void InitNLSFields(HWND hDlg, LCID lcid) { TCHAR tcsTemp[MAX_STR]; HWND hList = NULL; // Init larg number fields... LoadString(g_hInst, STR_LARGEPOSNUMBER, g_tcsTemp, MAX_STR); GetNumberFormat(lcid, 0, g_tcsTemp, NULL, tcsTemp, MAX_STR); SetWindowText(GetDlgItem(hDlg, IDC_POS_NUMBER), tcsTemp); LoadString(g_hInst, STR_LARGENEGNUMBER, g_tcsTemp, MAX_STR); GetNumberFormat(lcid, 0, g_tcsTemp, NULL, tcsTemp, MAX_STR); SetWindowText(GetDlgItem(hDlg, IDC_NEG_NUMBER), tcsTemp); // Init the currency field format... LoadString(g_hInst, STR_LARGEPOSNUMBER, g_tcsTemp, MAX_STR); GetCurrencyFormat(lcid, 0, g_tcsTemp, NULL, tcsTemp, MAX_STR); SetWindowText(GetDlgItem(hDlg, IDC_POS_CURRENCY), tcsTemp); LoadString(g_hInst, STR_LARGENEGNUMBER, g_tcsTemp, MAX_STR); GetCurrencyFormat(lcid, 0, g_tcsTemp, NULL, tcsTemp, MAX_STR); SetWindowText(GetDlgItem(hDlg, IDC_NEG_CURRENCY), tcsTemp); // Init time field format... // Delete our previous list of items... hList = GetDlgItem(g_hDlg, IDC_TIMEFORMAT); if(NULL != hList) { SendMessage(hList, CB_RESETCONTENT , 0, 0); // Enumerates the time formats that are available for a specified locale. EnumTimeFormats(EnumTimeFormatsProc, lcid, 0); SendMessage(hList, CB_SETCURSEL, 0, 0); // item index SendMessage(hList, CB_GETLBTEXT, 0, (LPARAM) g_tcsTemp); GetTimeFormat(lcid, 0, NULL, g_tcsTemp, tcsTemp, MAX_STR); SetWindowText(GetDlgItem(hDlg, IDC_TIMESAMPLE), tcsTemp); } // Init calendar field format... // Delete our previous list of items... hList = GetDlgItem(g_hDlg, IDC_CALFORMAT); if(NULL != hList) { SendMessage(hList, CB_RESETCONTENT , 0, 0); // Enumerates the short date formats that are available for a specified locale. EnumCalendarInfo(EnumCalendarInfoProc, lcid, ENUM_ALL_CALENDARS, CAL_SCALNAME); SendMessage(hList, CB_SETCURSEL, 0, 0); // item index SendMessage(hList, CB_GETLBTEXT, 0, (LPARAM) g_tcsTemp); } // Init short date field format... // Delete our previous list of items... hList = GetDlgItem(g_hDlg, IDC_SDATEFORMAT); if(NULL != hList) { SendMessage(hList, CB_RESETCONTENT , 0, 0); // Enumerates the short date formats that are available for a specified locale. EnumDateFormats(EnumSDateFormatsProc, lcid, DATE_SHORTDATE); SendMessage(hList, CB_SETCURSEL, 0, 0); // item index SendMessage(hList, CB_GETLBTEXT, 0, (LPARAM) g_tcsTemp); GetDateFormat(lcid, 0, NULL, g_tcsTemp, tcsTemp, MAX_STR); SetWindowText(GetDlgItem(hDlg, IDC_SDATESAMPLE), tcsTemp); } // Init long date field format... // Delete our previous list of items... hList = GetDlgItem(g_hDlg, IDC_LDATEFORMAT); if(NULL != hList) { SendMessage(hList, CB_RESETCONTENT , 0, 0); // Enumerates the long date formats that are available for a specified locale EnumDateFormats(EnumLDateFormatsProc, lcid, DATE_LONGDATE); SendMessage(hList, CB_SETCURSEL, 0, 0); // item index SendMessage(hList, CB_GETLBTEXT, 0, (LPARAM) g_tcsTemp); GetDateFormat(lcid, 0, NULL, g_tcsTemp, tcsTemp, MAX_STR); SetWindowText(GetDlgItem(hDlg, IDC_LDATESAMPLE), tcsTemp); } }