Solved

Numeric Formatting

Posted on 2002-06-24
13
470 Views
Last Modified: 2013-11-20
How do I format numerics into strings using GetNumberFormat().


I am most interested in how to get the +/- signs to appear where needed.... I am having a hard time getting +1.231 to come out properly...
0
Comment
Question by:Dak_Programmer
  • 6
  • 3
  • 2
  • +2
13 Comments
 
LVL 16

Expert Comment

by:AlexNek
ID: 7104318
Try to change
HKEY_CURRENT_USER\Control Panel\International\sPositiveSign
to '+'
0
 

Author Comment

by:Dak_Programmer
ID: 7104327
yes - I could go that route -- but I want this to work for different all other locales as well....
0
 
LVL 4

Expert Comment

by:mblat
ID: 7104386
What SetLocaleInfo with LOCALE_SPOSITIVESIGN as locale type doesn;t work for you?
0
 

Author Comment

by:Dak_Programmer
ID: 7104393
nope here is my code -- no plus signs appear with this code...

     strcpy(lpszValue, "21121.212");

     
     LCID lcid = m_comboLocale.GetItemData(nIndex);

     // create a locale helper
     CLocaleInfo locale(lcid);
     
     // construct a NUMBERFMT structure
     NUMBERFMT nmFormat;
     nmFormat.NumDigits = 4;
     nmFormat.LeadingZero = locale.GetLeadZeros();
     nmFormat.Grouping = locale.GetGrouping();
     
     TCHAR lpszDec[10];
     
     // get the decimal separator
     locale.GetDecimalSep(lpszDec, 10);
     nmFormat.lpDecimalSep = lpszDec;
     
     // see if we should use the thousands separator
     TCHAR lpszThousands[10]=_T("");
     locale.GetCurrencyThousandsSep(lpszThousands, 10);
     nmFormat.lpThousandSep = lpszThousands;
     
     nmFormat.NegativeOrder = locale.GetNegOrder();
     ::SetLocaleInfo(lcid, LOCALE_SPOSITIVESIGN, "+");
     // format the number
     ::GetNumberFormat(lcid, 0, lpszValue, &nmFormat, lpszNumber, nMax);
0
 
LVL 16

Expert Comment

by:AlexNek
ID: 7105432
2 Dak_Programmer
It seems that Microsoft simple ignore LOCALE_SPOSITIVESIGN.
MSDN: "sPositiveSign ... If an application calls the GetNumberFormat or GetCurrencyFormat API, the system will use the default symbol for the locale." (default symbol = blank)

Try to add positive sign yourself.
0
 

Author Comment

by:Dak_Programmer
ID: 7105478
so you are telling me that there is no way to get GetNumberFormat to return me a +1.21 -- that sucks
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 16

Expert Comment

by:AlexNek
ID: 7105547
>there is no way
To be correct - I can't see now the legal way to do it.
This info is only in the MSDN CD not in msdn.microsoft.com
http://www.tburke.net/info/regentry/69564.htm
0
 

Expert Comment

by:papaya_zm
ID: 7106043
you can do like this:
Add()
{
int number=1.231;
CString CStrnumber;
CStrnumber.Format("+%d",number);
}
so the CStrnumber="+1.231";
0
 

Author Comment

by:Dak_Programmer
ID: 7107517
This doesn't take into account locale information....
0
 
LVL 4

Expert Comment

by:mblat
ID: 7109010
Well, unfortuanatly the only way I see how it can work like this:

TCHAR szPlusSignBuffer[6];
GetLocaleInfo(whater locale you are looking at,LOCALE_SPOSITIVESIGN,szPlusSignBuffer,6);

.....
int ix;

.....
::GetNumberFormat(lcid, 0, lpszValue, &nmFormat, lpszNumber, nMax);

.....
if(ix > 0)
    sprintf(MyFinalBuffer,"%s%s",szPlusSignBuffer,lpszNumber);

Yes I know it's not exactly what you were looking for, but at this moment I can't help you more that that :-(

0
 

Author Comment

by:Dak_Programmer
ID: 7109051
Well I am still hoping that there is a better method for this...

0
 
LVL 49

Accepted Solution

by:
DanRollins earned 200 total points
ID: 7115926
You can use GetCurrencyFormat rather than GetNumberFormat.  Set
   CURRENCYFMT.lpCurrencySymbol= "+";

But this whole discussion is out of whack.  What international locale expects a leading plus sign on positive values?  I'll answer: None.   So you are doing some oddball mod to the number for your own reasons.  In that case, just stick a "+" in front of the number whenever it is a positive value.

-- Dan
0
 

Author Comment

by:Dak_Programmer
ID: 7246908
thanks for your comments -- and I guess I agree with your final point... maybe I am trying to do something out of whack -- but it to me it seems like something someone may like to do... thanks for your help anyways
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
bobThere java chalenge 45 91
Looking for a specific application/software 2 104
Best tools for Backup and restore of Wordpress site 6 55
dog bark java program 15 88
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now