mirror of
https://github.com/Gnucash/gnucash.git
synced 2025-02-25 18:55:30 -06:00
minor fixes to amount parsing algorithm
git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@495 57a11ea4-9604-0410-9ed3-97b8803252fd
This commit is contained in:
parent
f79f8f4117
commit
8407a1104c
102
src/util.c
102
src/util.c
@ -23,6 +23,8 @@
|
|||||||
* Huntington Beach, CA 92648-4632 *
|
* Huntington Beach, CA 92648-4632 *
|
||||||
\********************************************************************/
|
\********************************************************************/
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "messages.h"
|
#include "messages.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
@ -122,89 +124,87 @@ char * xaccPrintAmount (double val, short shrs)
|
|||||||
* doing. Don't break it!
|
* doing. Don't break it!
|
||||||
\********************************************************************/
|
\********************************************************************/
|
||||||
|
|
||||||
double xaccParseUSAmount (char * str)
|
#define MINUS_SIGN '-'
|
||||||
|
#define K_SEP ',' /* thousands separator */
|
||||||
|
#define DEC_SEP '.' /* decimal point */
|
||||||
|
|
||||||
|
double xaccParseUSAmount (const char * instr)
|
||||||
{
|
{
|
||||||
char * tok;
|
char *mstr, *str, *tok;
|
||||||
double dollars = 0.0;
|
double dollars = 0.0;
|
||||||
int len;
|
int len;
|
||||||
int isneg = 0;
|
int isneg = 0;
|
||||||
|
|
||||||
if (!str) return 0.0;
|
if (!instr) return 0.0;
|
||||||
|
mstr = strdup (instr);
|
||||||
|
str = mstr;
|
||||||
|
|
||||||
if ('-' == str[0]) {
|
/* strip off garbage at end of the line */
|
||||||
isneg = 1;
|
tok = strchr (str, '\r');
|
||||||
str += sizeof(char);
|
if (tok) *tok = 0x0;
|
||||||
}
|
tok = strchr (str, '\n');
|
||||||
|
if (tok) *tok = 0x0;
|
||||||
|
|
||||||
tok = strchr (str, ',');
|
/* search for a minus sign */
|
||||||
|
tok = strchr (str, MINUS_SIGN);
|
||||||
if (tok) {
|
if (tok) {
|
||||||
*tok = 0x0;
|
isneg = 1;
|
||||||
dollars = ((double) (1000 * atoi (str)));
|
|
||||||
str = tok+sizeof(char);
|
str = tok+sizeof(char);
|
||||||
}
|
}
|
||||||
|
|
||||||
tok = strchr (str, ',');
|
/* remove comma's */
|
||||||
if (tok) {
|
tok = strchr (str, K_SEP);
|
||||||
|
while (tok) {
|
||||||
*tok = 0x0;
|
*tok = 0x0;
|
||||||
dollars *= 1000.0;
|
dollars *= 1000.0;
|
||||||
dollars += ((double) (1000 * atoi (str)));
|
dollars += ((double) (1000 * atoi (str)));
|
||||||
str = tok+sizeof(char);
|
str = tok+sizeof(char);
|
||||||
}
|
}
|
||||||
|
|
||||||
tok = strchr (str, '.');
|
/* search for a decimal point */
|
||||||
|
tok = strchr (str, DEC_SEP);
|
||||||
if (tok) {
|
if (tok) {
|
||||||
*tok = 0x0;
|
*tok = 0x0;
|
||||||
dollars += ((double) (atoi (str)));
|
dollars += ((double) (atoi (str)));
|
||||||
str = tok+sizeof(char);
|
str = tok+sizeof(char);
|
||||||
|
|
||||||
tok = strchr (str, '\r');
|
/* if there is anything trailing the decimal
|
||||||
if (!tok) {
|
* point, convert it */
|
||||||
tok = strchr (str, '\n');
|
if (str[0]) {
|
||||||
if (!tok) return dollars;
|
|
||||||
}
|
|
||||||
*tok = 0x0;
|
|
||||||
|
|
||||||
/* strip off garbage at end of the line */
|
/* strip off garbage at end of the line */
|
||||||
tok = strchr (str, '\n');
|
tok = strchr (str, ' ');
|
||||||
if (tok) *tok = 0x0;
|
if (tok) *tok = 0x0;
|
||||||
|
|
||||||
tok = strchr (str, ' ');
|
/* adjust for number of decimal places */
|
||||||
if (tok) *tok = 0x0;
|
len = strlen(str);
|
||||||
|
if (6 == len) {
|
||||||
/* adjust for number of decimal places */
|
dollars += 0.000001 * ((double) atoi (str));
|
||||||
len = strlen(str);
|
} else
|
||||||
if (6 == len) {
|
if (5 == len) {
|
||||||
dollars += 0.000001 * ((double) atoi (str));
|
dollars += 0.00001 * ((double) atoi (str));
|
||||||
} else
|
} else
|
||||||
if (5 == len) {
|
if (4 == len) {
|
||||||
dollars += 0.00001 * ((double) atoi (str));
|
dollars += 0.0001 * ((double) atoi (str));
|
||||||
} else
|
} else
|
||||||
if (4 == len) {
|
if (3 == len) {
|
||||||
dollars += 0.0001 * ((double) atoi (str));
|
dollars += 0.001 * ((double) atoi (str));
|
||||||
} else
|
} else
|
||||||
if (3 == len) {
|
if (2 == len) {
|
||||||
dollars += 0.001 * ((double) atoi (str));
|
dollars += 0.01 * ((double) atoi (str));
|
||||||
} else
|
} else
|
||||||
if (2 == len) {
|
if (1 == len) {
|
||||||
dollars += 0.01 * ((double) atoi (str));
|
dollars += 0.1 * ((double) atoi (str));
|
||||||
} else
|
}
|
||||||
if (1 == len) {
|
|
||||||
dollars += 0.1 * ((double) atoi (str));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
tok = strchr (str, '\r');
|
|
||||||
if (tok) *tok = 0x0;
|
|
||||||
tok = strchr (str, '\n');
|
|
||||||
if (tok) *tok = 0x0;
|
|
||||||
tok = strchr (str, ' ');
|
|
||||||
if (tok) *tok = 0x0;
|
|
||||||
|
|
||||||
dollars += ((double) (atoi (str)));
|
dollars += ((double) (atoi (str)));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isneg) dollars = -dollars;
|
if (isneg) dollars = -dollars;
|
||||||
|
|
||||||
|
free (mstr);
|
||||||
return dollars;
|
return dollars;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user