Best Way To Handle Floating Points

I have a floating point field (dollar amount) to put on the screen.  What is
the best way to handle this?  I defined a variable as a floating point but
when I entered 99,999,999.99 I noticed it returned the number as an
exponential number.  Is this normal?  I guess I thought I would have seen it
as I entered it.

My logic may be right - I just want to make sure.  Is there a way to define
a mask so the commas are inserted automatically?

-- 
Edward Fultz
Sr. Software Engineer
Pamet Systems
Acton, Ma
www.pametsystems.com


0
11/17/2004 12:42:24 AM
vc.mfc 33608 articles. 0 followers. Follow

5 Replies
591 Views

Similar Articles

[PageSpeed] 56

Unless you really need floating point for some reason, I would store the 
information as LONGLONG integers just multiplied times 100.  That way it's 
easier to add (no round off problems) and you can always /100 to get just 
the dollars or %100 to get the cents.

Here is some code for adding commas (or whatever is used in your neck of the 
woods).  This doesn't do dollars and cents, but that's a pretty easy 
modification.

//
// Add commas to a number in a buffer (for display purposes)
//
TCHAR *Comma(LONGLONG value,TCHAR *bufout, UINT nSize)
{
    TCHAR bufin[30]; // 30 digits is a really big number
    TCHAR dsep[5], tsep[5];
    NUMBERFMT fmt;
    fmt.NumDigits = 0;
    ::GetLocaleInfo(LOCALE_USER_DEFAULT,LOCALE_ILZERO,bufin,2);
    fmt.LeadingZero = _ttoi(bufin);
    fmt.Grouping = 3;
    ::GetLocaleInfo(LOCALE_USER_DEFAULT,LOCALE_SDECIMAL,dsep,4);
    fmt.lpDecimalSep = dsep;
    ::GetLocaleInfo(LOCALE_USER_DEFAULT,LOCALE_STHOUSAND ,tsep,4);
    fmt.lpThousandSep = tsep;
    ::GetLocaleInfo(LOCALE_USER_DEFAULT,LOCALE_INEGNUMBER ,bufin,2);
    fmt.NegativeOrder = _ttoi(bufin);
    sprintf(bufin,"%I64d",value);
    ::GetNumberFormat(LOCALE_USER_DEFAULT, 0, bufin, &fmt, bufout, nSize);
    return bufout;
}

HTH,

Tom

"Ed Fultz" <edward.fultz@comcast.net> wrote in message 
news:uA2hP5DzEHA.3840@tk2msftngp13.phx.gbl...
>I have a floating point field (dollar amount) to put on the screen.  What 
>is
> the best way to handle this?  I defined a variable as a floating point but
> when I entered 99,999,999.99 I noticed it returned the number as an
> exponential number.  Is this normal?  I guess I thought I would have seen 
> it
> as I entered it.
>
> My logic may be right - I just want to make sure.  Is there a way to 
> define
> a mask so the commas are inserted automatically?
>
> -- 
> Edward Fultz
> Sr. Software Engineer
> Pamet Systems
> Acton, Ma
> www.pametsystems.com
>
> 


0
tserface (3861)
11/17/2004 1:09:29 AM
Well, the only reason I need a float is because this is a dollar field.  The
logic you provided for putting commas - thanks.  I thought about how to
handle that and I think if I do an OnChange and as it does the entry I can
put in commas - since it is presentation only.  Not so critical at the
moment until I get it storing and returning the dollar amount properly.

-- 
Edward Fultz
Sr. Software Engineer
Pamet Systems
Acton, Ma
www.pametsystems.com

"Tom Serface" <tserface@msn.com> wrote in message
news:erNDZIEzEHA.1300@TK2MSFTNGP14.phx.gbl...
> Unless you really need floating point for some reason, I would store the
> information as LONGLONG integers just multiplied times 100.  That way it's
> easier to add (no round off problems) and you can always /100 to get just
> the dollars or %100 to get the cents.
>
> Here is some code for adding commas (or whatever is used in your neck of
the
> woods).  This doesn't do dollars and cents, but that's a pretty easy
> modification.
>
> //
> // Add commas to a number in a buffer (for display purposes)
> //
> TCHAR *Comma(LONGLONG value,TCHAR *bufout, UINT nSize)
> {
>     TCHAR bufin[30]; // 30 digits is a really big number
>     TCHAR dsep[5], tsep[5];
>     NUMBERFMT fmt;
>     fmt.NumDigits = 0;
>     ::GetLocaleInfo(LOCALE_USER_DEFAULT,LOCALE_ILZERO,bufin,2);
>     fmt.LeadingZero = _ttoi(bufin);
>     fmt.Grouping = 3;
>     ::GetLocaleInfo(LOCALE_USER_DEFAULT,LOCALE_SDECIMAL,dsep,4);
>     fmt.lpDecimalSep = dsep;
>     ::GetLocaleInfo(LOCALE_USER_DEFAULT,LOCALE_STHOUSAND ,tsep,4);
>     fmt.lpThousandSep = tsep;
>     ::GetLocaleInfo(LOCALE_USER_DEFAULT,LOCALE_INEGNUMBER ,bufin,2);
>     fmt.NegativeOrder = _ttoi(bufin);
>     sprintf(bufin,"%I64d",value);
>     ::GetNumberFormat(LOCALE_USER_DEFAULT, 0, bufin, &fmt, bufout, nSize);
>     return bufout;
> }
>
> HTH,
>
> Tom
>
> "Ed Fultz" <edward.fultz@comcast.net> wrote in message
> news:uA2hP5DzEHA.3840@tk2msftngp13.phx.gbl...
> >I have a floating point field (dollar amount) to put on the screen.  What
> >is
> > the best way to handle this?  I defined a variable as a floating point
but
> > when I entered 99,999,999.99 I noticed it returned the number as an
> > exponential number.  Is this normal?  I guess I thought I would have
seen
> > it
> > as I entered it.
> >
> > My logic may be right - I just want to make sure.  Is there a way to
> > define
> > a mask so the commas are inserted automatically?
> >
> > -- 
> > Edward Fultz
> > Sr. Software Engineer
> > Pamet Systems
> > Acton, Ma
> > www.pametsystems.com
> >
> >
>
>


0
11/17/2004 1:17:54 AM
"Ed Fultz" <edward.fultz@comcast.net> wrote:

>I have a floating point field (dollar amount) to put on the screen.  What is
>the best way to handle this?  I defined a variable as a floating point but
>when I entered 99,999,999.99 I noticed it returned the number as an
>exponential number.  Is this normal?  I guess I thought I would have seen it
>as I entered it.

Floating point numbers are stored as a number and an exponent of 2. So
they are similar to numbers in scientific notation. But scientific
notation uses powers of ten (1.23 times 10**23, for example), and
computer floating point numbers use powers of two (1.12345 time
2**234). This representation allows you to represent an approximation
of extremely large or small numbers. There are various ways to convert
a floating point number to a string for output to humans.

Since floats are approximations, you probably don't want to use them
to keep track of dollars and cents. The COleCurrency MFC class may
suit your needs better
(http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_MFC_COleCurrency.asp)

-- 
Tim Slattery
MS MVP(DTS)
Slattery_T@bls.gov
0
Slattery_T (112)
11/17/2004 1:44:48 PM
Thanks for the update.  What data type would be best for the database?  The
DECIMAL data type, maybe?

-- 
Edward Fultz
Sr. Software Engineer
Pamet Systems
Acton, Ma
www.pametsystems.com

"Tim Slattery" <Slattery_T@bls.gov> wrote in message
news:k3lmp0hikpe9vdmm602if380t24s8d72oc@4ax.com...
> "Ed Fultz" <edward.fultz@comcast.net> wrote:
>
> >I have a floating point field (dollar amount) to put on the screen.  What
is
> >the best way to handle this?  I defined a variable as a floating point
but
> >when I entered 99,999,999.99 I noticed it returned the number as an
> >exponential number.  Is this normal?  I guess I thought I would have seen
it
> >as I entered it.
>
> Floating point numbers are stored as a number and an exponent of 2. So
> they are similar to numbers in scientific notation. But scientific
> notation uses powers of ten (1.23 times 10**23, for example), and
> computer floating point numbers use powers of two (1.12345 time
> 2**234). This representation allows you to represent an approximation
> of extremely large or small numbers. There are various ways to convert
> a floating point number to a string for output to humans.
>
> Since floats are approximations, you probably don't want to use them
> to keep track of dollars and cents. The COleCurrency MFC class may
> suit your needs better
>
(http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html
/_MFC_COleCurrency.asp)
>
> -- 
> Tim Slattery
> MS MVP(DTS)
> Slattery_T@bls.gov


0
11/17/2004 6:09:28 PM
The database should use money or smallmoney datatypes to keep track of
money.
0
11/17/2004 8:31:32 PM
Reply:

Similar Artilces:

Table floating off page in relationships window
Microsoft Access - in my relationships window, one of my tables has gone off the page and I can't move it down. I scrolled to the top of the page and it is still cutting the top of the table off so I can't move it down. Any suggestions on how to rearrange the tables in the relationships window without physically dragging them around the screen? There are several methods to fix this issue. 1) Set your screen res to the next highest setting and then then Maximize your Relationship window. 2) From Tony and Jeff: >Could you <tab> to the offending table/window? If so, and ...

point of sale & grocery store
can we use the POS 2.0 (not the RMS) for a grocery store with one checkout lane that weighs fruits and vegetables?on the comparison chart between RMS and POS it mentions that POS cannot manage assemly,weighed and gasoline item types.just want to make sure. We've had good success with grocery stores. Just remember to use an RMS compliant scale! -- -Jerry JR Data Inc. "rms26" wrote: > can we use the POS 2.0 (not the RMS) for a grocery store with one checkout > lane that weighs fruits and vegetables?on the comparison chart between RMS > and POS it mentions that P...

Tool to find Handle Leaks
Hello, Anybody knows some good tool to find handle leaks? Thanks & Regards VSP VSP wrote: > Hello, > > Anybody knows some good tool to find handle leaks? > > Thanks & Regards > VSP Unfortunately there's probably not going to be a tool that will scan your application and find out exactly where it's leaking handles. However, if you're up for getting a little down and dirty, you can run your program, and then try to find out what handles aren't being released with the SysInternals Handle software - http://www.sysinternals.com/utilities/handle.html...

Why a movie clip in power point plays too fast?
I have inserted a movie clip into a power point presentation but it plays too fast and without sound. What do I have to do for viewing the clip at normal speed and sound? "William" <William@discussions.microsoft.com> wrote in message news:A53BB2D5-B8C9-4815-AB55-C855BA92BE8B@microsoft.com... >I have inserted a movie clip into a power point presentation but it plays >too > fast and without sound. What do I have to do for viewing the clip at > normal > speed and sound? Hi William It could be missing codecs. There is a freebie called GSpot th...

Best approach to customized styles
I have seen various installations of Word (2007, 2002, 2000) lose some of the custom settings for styles I have assigned to them. I haven't seen this mentioned before so I can only assume that I'm one of the few that this is happening to. I primarily use Quote, Headings 1-5, and Body Text. I suppose you can throw in footnote as well. Body text seems to be fine, but I am continually having to reset my preferences for the headings styles. THey will work for a few weeks but not after that. I would like to take a final stab at this--would I be better off simply creating a new ...

Is there a best practices FAQ, for using Volume Shadow Copy with Exchange 2003 databases?
We have Exchange 2003 Enterprise running on Windows 2003 Standard. I would like to start taking 'snapshots' of the log and database volumes regularly (4 times daily), as a way to do immediate interval restores of our Exchange DBs rather than having to dump from tape. Is there a best practices document, for doing this? Thanks in advance. NTBackup does not do snapshots of Exchange. You would need to find a 3rd party backup product that supports that. -- Ben Winzenz Exchange MVP MessageOne "Robert Gordon" <Robert.Gordon@nospam.dslextreme.com> wrote in message ...

Gettting divorced
After 15 yrs of marriage I'm getting divorced. My spouse is no longer willing to share her transaction data with me. I could start a new file, but I would prefer to keep my current file, which has 5 years of data in it. Since my spouse is not willing to give me the data to properly handle the divorce in MS Money, I have the following question What category should I use for the adjustments required to bring my spouse's 401K and stock options accounts to $0? I checked the FAQ, but there's no section on divorce and how to handle things in Money. Dick, any suggestions? As I go...

FRx
Cost of sales is calculated as Opening Stock+Purchase-Closing Stock. Opening/Closing stock are entered and kept in an UNPOST GL Trx Batch and FRx reported is generated with Unpost Trx included. The issue: Jan Opening Stock=0.00 Jan Closing Stock=$100 Feb Opening Stock=$100 (always equal closing stock of previous mth) Feb Closing Stock=$150 Mar Opening Stock=$150 Mar Closing Stock=$200 for FRx to print P&L with CUR, * opening stock=opening stock of that period * closing stock=closing stock of that period with above trx, FRx will report correct figure. now, the issue is with printing ...

Best way to upgrade OEM software?
I am looking at buying a few laptops but they all seem to ship with Windows 7 Home and some with MS Office 2007 Home & Student. Can anyone suggest the best way 'upgrade' these to XP Pro and MS Office 2003 Pro? To reduce the maintainence burden I would like to keep them the same as the rest of our clients network. Thanks, Nick Hi Nick: For this group, your post is OT (off topic). Not that OT posts are not allowed, but it is customary to identify then as such. However, to answer your question: There are a great many systems that ship with your choi...

Best way of copy range of cells without select
Hi, Why doesn't this work? "Method 'range' in object sheet failed" . I try to get rid of the copy paste select activat - operations.... Blad105.Range("B6:B25,B29:B44,B52:B56") = Blad104.Range("B6:B25,B29:B44,B52:B56") /Thanks to any help I answer myself: Has to be a ".value" after the specified range - othervice it try to copy the range, and it can't define the same worksheet name on two sheets... /"" The value is needed baecause "Imbecill" <Imbecill@roll.se> skrev i meddelandet news:Kj_oc.59034$mU6....

What is the best online consolidator
What is the best online consolidator to produce a single page view of numerous bank and brokerage accounts. I understand that each will remain seperate. On Jun 19, 4:09=A0pm, "WOppenhe" <woppe...@ucla.edu> wrote: > What is the best online consolidator to produce a single page view of > numerous bank and brokerage accounts. =A0I understand that each will rema= in > seperate. I've played with Yodlee for a few years - it keeps getting better and better ...

Seiko Chronograph Steel Mens Watch SNA795, Best Wristwatch World
Seiko Chronograph Steel Mens Watch SNA795, Best Wristwatch World Click Here To Website : http://www.watchebay.net/Seiko-Chronograph-Steel-Mens-Watch-SNA795.html Wristwatch World: http://www.watchebay.net/ Seiko Chronograph Steel Mens Watch SNA795 Information : Brand : Seiko Watches ( http://www.watchebay.net/Seiko-Watches.html ) Gender : Mens Code : Seiko-Chrono-Mens-Watch-SNA795 Also Called : SNA795, SNA795-1, SNA795-P1, SNA795P1, Seiko- SNA795, Seiko-SNA795-1 Case Material : Stainless Steel Case Thickness : Dial Color : White Bezel : ...

CRecordSet
I'm converting from DAO to ODBC in C++. One of the problems I ran into is that DAO provides a DFX_CURRENCY exchange and ODBC doesn't have a RFX_CURRENCY. How do you handle currency/decimal types in ODBC data exhange? Thanks, Neil PS: Since MS is dropping support for DAO is ODBC the right direction to go or is there a better recommendation? You need to use RFX_Double for currency. AliR. "Neil B" <NeilB@discussions.microsoft.com> wrote in message news:780C85BD-2DF8-4241-A59A-D67E27C37639@microsoft.com... > I'm converting from DAO to ODBC in C++. One of th...

unfamiliar with how Excel handles data
I inherited a work excel spreadsheet from an employee who was fired and has no desire to help the company. My question: There is a table of info, say with: name, address, phone number, fax number, email. If you place the cursor in a box on the spreadsheet (different page) and type a number - say 1 corresponding to line 1 on the table - the info will then populate certain fields on the spreadsheet. Or, if I type "1" I get Joe Smith, 1600 Pennsylvania Ave, phone, fax & email in 5 different fields. The number 1 does not actually appear on the spreadsheet, it simply "t...

How to handle a minus sign "-" as text not as part of a formula?
When I enter something into a cell beginning with a minus sign "-" then the content is automatically interpreted as a formula. How can I prevent this and let Excel treat the cell content (only for this cell) as text? Claudia You can start your entry with an apostrophe: '-1234 qwer or you could preformat the cell(s) as Text, then do the data entry. Claudia d'Amato wrote: > > When I enter something into a cell beginning with a minus sign "-" then the content > is automatically interpreted as a formula. > > How can I prevent this...

"Share balance negative at some point"
I am trying to record a sale of some shares of a fund that I own. I have over 2900 shares of this fund and I am trying to record the sale of 68 shares. When I try to record it as a "sell" transaction I get an error message that says "The changes you are making will cause the share balance for 'Fundxxx" to become negative at some point. Please cancel or revise the transaction." The issue only affects this one fund and it will allow me to record the transaction as a "short sell". I do not understand why it will not let me record the transaction? Any ...

Connecting Points In An XY-Scatter Plot Graph
I made a data table to help me organize my workout schedule at the gym. On the X-axis I used the date, and for the y-axis I have weight. I have several different excersizes, and thus several different plots (on the same graph). I don't do every excersize every time so the points are some what scattered. The graph is a "XY (Scatter) with data points connected by lines." Ok. My problem is that the points of a line are only connected when I do the same excercise twice in a row (not row as in column). For example; on 04/06/06 and 04/08/06 I do bicep curls, the points are conne...

DPM 2007 Copy Specific Recovery Points To Tape
Hello, I was wondering if it is possible to copy specific recovery points to one tape. In the console GUI if I go to the recovery tab and specify a recovery point I have the option to "Copy to tape". The limitation there is I seem to be able to only copy the one recovery point to a tape regardless of size. These recovery points belong to different protection groups also. Can this be done through Powershell? Thanks Here is an example of how I am copying a pg[0], ds[0], rp[0] to a library lib[0] to an empty tape by creating a tape label DPMTape1. Just a note he...

The way numbers are displayed
I have a price sheet that displays prices like this 6.26248371689101 need to change them to display $6.26 have tried currency, accounting, everything. Please help Thanks for any help! It looks like your "numbers" are in fact text. With the "number" in A1, put this formula in another cell: =VALUE(A1) You can format that cell the way you like -- Kind regards, Niek Otten Microsoft MVP - Excel "Pnw" <Pnw@discussions.microsoft.com> wrote in message news:88FBFADE-4DE6-4974-9B15-72116946BC95@microsoft.com... |I have a price sheet that displays prices li...

Best Practice: Business Units or automatic sharing??
Hi, We have following scenario in which users are granted as less access to records as possible. The sales persons are subdivided per country area (call it 'sales territory'). Sales persons can only see their own records. Every area will also have two managers, which can see all accounts they created and all accounts the sales persons of that area created. Furthermore it is possible that one person is a manager of several sales areas... What is according to you people the best practice to set up this security model? The difficulty here are not really the 'sales persons&#...

Is there a way to find the size of each database object?
Hi All I have a reasonably simple database but even after running "Compact & Repair" it seems the whole .mdb file is much larger than it should be compared to other MS Access database that I have created. Is there an easy way to find the size of each database object? I have forms with VB code and reports and macros. I have started a new .mdb file and importing the objects over one by one to find the culprit but this is (of course) a long and painful process. I performed a similar test where I imported each set of objects (tables, queries, forms, reports, macros ...

Is there a way to delete all emails w/o deleting the account?
Hi, Is there a way to clean up an email account in Exchange 2000 Std. without deleting the account other than logging into it and deleting all the emails? We are having trouble logging into this account through Outlook or OWA. The user never maintained her email account. There seems to be thousands of items in her mailbox. I don't know if they're all emails or there are some calendar items, etc. too. I assume what the System Manager shows me includes everyting in the mailbox i.e. calendar items, contacts, tasks, etc. Is there a tool that I can utilize to clean up this mailbox? ...

a simple way to set up spreadsheet for conference rooms reservatio
conference room reservations wrote: <Nothing> Please write your question in the BODY of the post, not the subject as that can be truncated just like yours has. I don't think Excel would be the best way of doing this. Have you got Outlook? I got a lot of my begginer help on this site... cool they teach you by making a, time sheet, Invoice and cheek book from scratch on Windows media Player very cool www.MDOTutorials.com -- Route2391 ------------------------------------------------------------------------ Route2391's Profile: http://www.excelforum.com/member....

Deleted items retention best practices?
Are there any standard policies regarding deleted item retention in 5.5? Things like a suggested length of the rention time? Configurations for ease of administration and most practical use of hardware resources? Thanks and thanks in advance for any suggestions, Mike Depends on disk space, whether you're using standard/enterprise, how much you can back up/restore. I usually do 15 to 30 days for a tiny office - I'd say 5 days is the minimum I'd use. myrangersstink wrote: > Are there any standard policies regarding deleted item retention in > 5.5? Things like a suggested ...

Jaeger-LeCoultre Reverso Classique Mens Watch 279.81.70, Best Wristwatch World
Jaeger-LeCoultre Reverso Classique Mens Watch 279.81.70, Best Wristwatch World Click Here To Website : http://www.watchebay.net/Jaeger-LeCoultre-Reverso-Classique-Mens-Watch-279.81.70.html Wristwatch World: http://www.watchebay.net/ Jaeger-LeCoultre Reverso Classique Mens Watch 279.81.70 Information : Brand : Jaeger LeCoultre Watches ( http://www.watchebay.net/Jaeger-LeCoultre-Watches.html ) Gender : Mens Code : 279.81.70 Also Called : Case Material : Stainless Steel Case Thickness : Dial Color : Black Bezel : Stainless Steel Movement : ...