home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
QBasic & Borland Pascal & C
/
Delphi5.iso
/
C
/
Samples
/
CSAPE32.ARJ
/
SOURCE
/
CSSRC
/
FNDECPT.C
< prev
next >
Wrap
C/C++ Source or Header
|
1990-08-15
|
3KB
|
151 lines
/*
fndecp.c 11/18/86
% strdecp, strnodecp
Routines for using fixed decimal points commas in numeric fields.
C-scape 3.2
Copyright (c) 1986, 1987, 1988 by Oakland Group, Inc.
ALL RIGHTS RESERVED.
Revision History:
-----------------
5/13/88 jmd removed length restrictions
6/01/89 gam added ocountry stuff
3/28/90 jmd ansi-fied
6/13/90 jdc replaced backwards pointer math
8/15/90 pmcm fixed duplicate '-' problem
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "cscape.h"
#include "fnfunc.h" /* for field functions */
char *strdecp(char *s, int pos)
/*
Place a fixed decimal point at pos.
Removes leading zeroes (unless there is no other number left of the dec pt).
(pos = 2) [ -00000342]
becomes: [ -3.42]
*/
{
int count, minus, i, j;
char first;
/* remember first character */
first = *s;
/* go through the string thrice */
/* first add leading zeroes, (so that short strings work correctly) */
/* see if this is a negative number */
minus = FALSE;
for (i = 0;; i++) {
if (s[i] == ' ') {
s[i] = '0';
}
else if (s[i] == '-') {
s[i] = '0';
minus = TRUE;
}
else {
break;
}
}
/* second, add the decimal point (removing old decimal points) */
for (i = strlen(s) - 1, count = 0; i >= 0; ) {
if ((i > 0) && isdigit(s[i])) {
count++;
if ((count == pos) && (s[i - 1] != ocountry.dec_char)) {
/* slide over front of string (left), insert a point. */
i--; /* move to decp's slot */
for (j = 0; j < i; j++) {
s[j] = s[j + 1];
}
s[j] = ocountry.dec_char;
/* increment count so that all future decp's are bad */
count++;
}
i--;
}
else if (s[i] == ocountry.dec_char) {
if (count == pos) {
/* decp belongs */
i--;
/* increment count so that all future decp's are bad */
count++;
}
else {
/* remove old decp */
/* slide over front of string (right), insert a '0' at the beginning */
for (j = i; j > 0; j--) {
s[j] = s[j - 1];
}
*s = '0';
}
}
else {
i--;
}
}
/* now get rid of leading zeroes (assume no leading spaces) */
for(i = 0, j = 0; s[i] != '\0'; i++) {
if (s[i] != '0' || s[i + 1] == ocountry.dec_char || s[i + 1] == '\0') {
break;
}
else {
s[i] = ' ';
j++;
}
}
/* restore minus sign */
j--;
if (minus && (j >= 0)) {
s[j] = '-';
}
/* restore first char if possible */
if (*s == ' ' && first != '0' && !(first == '-' && minus)) {
*s = first;
}
return(s);
}
char *strnodecp(char *s)
/*
Removes the decimal point from a string.
Contracts strings towards the right.
[ 1234567.89]
becomes: [ 123456789]
*/
{
int i, j;
/* move through the string removing commas */
for (i = strlen(s) - 1; i >= 0; ) {
if (s[i] == ocountry.dec_char) {
/* slide over front of string, insert a space at the beginning */
for (j = i; j > 0; j--) {
s[j] = s[j - 1];
}
*s = ' ';
}
else {
i--;
}
}
return(s);
}