Subject |
Re: string with commas to numeric |
From |
Akshat Kapoor <akshat.kapoor@kapoorsons.in> |
Date |
Tue, 5 Mar 2024 20:12:48 +0530 |
Newsgroups |
dbase.getting-started |
Good Evening Charlie,
>
> form.spinboxestv1.value = val(co.fields["&grd."].value)
>
> The spinbox is numeric and co is a string. This works unless there are commas in the string. Then 17,234 becomes 17.
>
> How can I get the entire number without commas when the string has commas in it?
This is just for additional ideas since you have a working solution.
I was out of station so just started reading the post after many days.
The following routine is there in stringEx.cc of duflp
function Text2Num
/*
-------------------------------------------------------------
Programmer..: Akshat Kapoor
Date........: August, 2021
Notes.......: Converts a character string with a numeric
value to the appropriate numeric value
in dBASE. Created to deal with input from
a CSV file, where many different issues
come up with numeric formats. The difficulty
is if the string has the "thousands" separator
(123,456.12), and it is a string, the val()
function stops at the separator, and returns
123, instead of the full numeric value. Other
issues such as negative numbers in parens,
different decimal point symbols for different
parts of the world, make things even more fun.
Written for.: dBASE 2019
Rev. History: August, 2021 -- KJM -- with Akshat's permission
co-opted it for the Import Data routines in the
dUFLP.
Calls.......: None
Usage.......: Text2Num( <cNumber>, <cDecimal> )
Example.....: ? Text2Num( "123,456.12", "." ) --> 123456.12
? Text2Num( "123.456,12", "," ) --> 123456.12
? Text2Num( "(123.456,12)", "," ) --> -123456.12
Returns.....: numeric value with no formatting except for
decimal
Parameters..: cNumber = character string containing numeric value
with whatever formatting is in the string
cDecimal = decimal point character. Default is "."
NOTE: This is designed specifically for the purpose of converting
numeric values that are stored in strings, to their actual
numeric value. If you pass, say, a phone number string to this
function, the results will be odd, to say the least.
-------------------------------------------------------------
*/
parameters cStr, cDecimal
if pCount() == 0 // no parameters
msgbox( "Please pass the character string necessary to convert
to "+;
"a numeric value!", "Missing Parameter", 16 )
return 0
endif
if pCount() == 1 // only one parameter
cDecimal = "." // default of a period for the decimal point marker
endif
private cRetVal, cAllowed, a, cCurrent, cLocalDecimal
cLocalDecimal = set( "point" )
cRetVal = ''
// allowed characters in resulting string:
cAllowed = '0123456789-'
// loop through the character string, one character at a time:
for a = 1 to len(cStr)
cCurrent = substr(cStr,a,1)
if cCurrent $ cAllowed
cRetVal+= cCurrent
elseif cCurrent = '(' // we have a negative number: (123) is -123
cRetVal = '-'+cRetVal
elseif cCurrent = '%' // percentage, divide the value by 100
// 50% = 0.50
cRetVal = " " + (val(cRetVal)/100)
elseif cCurrent = cDecimal
cRetVal = cRetVal + cLocalDecimal // add the decimal point
symbol
endif
endfor
return val( cRetVal )
// End of method: Text2Num()
Regards
Akshat
|
|