Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Convert number to a char string
Message
 
 
To
08/12/2019 12:59:42
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
01672170
Message ID:
01672180
Views:
53
>>>>Hi,
>>>>
>>>>I am trying to create a generic way to convert any number to a string. Here are some examples, of the number and how they should be as a string:
>>>>
>>>>
>>>>2.4       ->  "2.4"
>>>>.00       -> ".00"
>>>>500.32337   -> "500.32337"
>>>>8.342    -> "8.342"
>>>>
>>>>
>>>>I tried TRANSFORM() but the second parameter has to match the number of decimal digits.
>>>>
>>>>Any suggestions?
>>>
>>>You need to signify the proposed size. In such cases, it's often desirable to use:
>>>
>>>CLEAR
>>>* 2.4
>>>? ALLTRIM(STR(2.4, 14, 1))
>>>
>>>* More generically
>>>* .00
>>>? JustDecimal(StrTrim(0.00, 2))
>>>
>>>* 500.32337
>>>? StrTrim(500.32337, 5)
>>>
>>>* 8.342
>>>? StrTrim(8.342, 3)
>>>
>>>
>>>
>>>FUNCTION StrTrim(tnVal, tnDecimals, tnWidth)
>>>RETURN ALLTRIM(STR(tnVal, IIF(TYPE("tnWidth") != "N", 14, tnWidth), IIF(TYPE("tnDecimals") != "N", SET("DECIMALS"), tnDecimals)))
>>>
>>>FUNCTION JustDecimal(tcValue)
>>>RETURN "." + GETWORDNUM(tcValue, 2, ".")
>>>
>>
>>Your StrTrim() is good; thank you. But it requires that I specify the number of decimals.
>
>Note that it does not require that you specify decimals. If you don't specify decimals, it uses the SET("DECIMALS") value by default.
>
>In addition, it's not possible to handle numbers the way you suggest due to the way computers store base-10 decimals from a base-2 binary "floating point" value. The values of 1/3 and 2/3, for example, illustrate that some things cannot be represented finitely, so it won't work. When rounded to a certain position, it may be possible to represent the value exactly, but a lot of times even rounding to some number of decimals results in a value that internally looks like "1.23999999999" when it's actually 1.24. The value for 1.24 won't coalesce into that fixed "1.24" quantity until it's applied to a scenario.
>
>See the attached spreadsheet to illustrate how this works in binary, and why it doesn't work well in base-10, and why you can't always use the format you're attempting to use in base-10. Type a value in the green box and you'll see how it changes in the binary representation, and why the value it stores is not the exact value you expect in base-10. Input the number "118.29" for example, to see how it cannot be exactly represented.
>
>>And I am trying to figure how to do it for any number.
>>That is, in my case, I have to SCAN records of a table where the column could have different values (as you see in my example above). And in each row, the numeric value has to be changed to the char; matching exactly.
>
>You could reference the FIELD() number for the fields in use, associated with AFIELDS() to extract information about the width for those records, and reference them.
>
>When you have a specific purpose, you can apply additional knowledge / constants, but to do it generically, you'll have to provide contextual information, such as using SET("DECIMALS") by default if you don't pass in the decimals width, or to manually specify it.

Thank you very much for the explanation and your input.
"The creative process is nothing but a series of crises." Isaac Bashevis Singer
"My experience is that as soon as people are old enough to know better, they don't know anything at all." Oscar Wilde
"If a nation values anything more than freedom, it will lose its freedom; and the irony of it is that if it is comfort or money that it values more, it will lose that too." W.Somerset Maugham
Previous
Reply
Map
View

Click here to load this message in the networking platform