Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Truncating a string

Status
Not open for further replies.

ejhemler

Programmer
Aug 30, 2002
68
0
0
US
Hello,
I'm trying to convert a numeric value to a string, but I don't want it to round up the decimal point. Is there a way to do this?

NumValue = 0.1275

I need the string to be "0.12"

If I use str(NumValue,4,2) the value is rounded to " 0.13". Maybe there is a way to truncate the string from the right side. If I converted the value using str(NumValue,6,4) I would get a " 0.1275" and then I could just truncate the last two digits. But I can't figure out a way to do this. Could anyone help please??

Thanks,
Erik
 
I've found that the left() function works in this case. It must be early. Thanks anyways, if you were looking to help though.
Erik
 
Using LEFT() such as this example will work for 4 decimal points:
Code:
* allow up to million billion and 4 decimals precision
nVal=0.1275
cVal=TRIM(STR(nVal,20,4))
? LEFT(cVal,LEN(cVal)-2)
Here is more generic code to handle decimals of varying length:
Code:
* allow at least thousand billion
* 1-7 decimals precision which may round
nVal=0.1275
nDec=6  && range allowed is 1-7 decimals
nCut=2  && cut-off to this decimal length
cVal=TRIM(STR(nVal,20,nDec))
? LEFT(cVal,AT(".",cVal)+nCut)
dbMark
 
This should work:

Code:
NumValue = 0.1275
StrVal = tran(INT(numValue / 0.01)*0.01)


- Bill

Get the best answers to your questions -- See FAQ481-4875.
 
or LEFT(TRANSFORM(FLOOR(NumValue*100)/100),4)

Brian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top