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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to extract value after decimal

Status
Not open for further replies.

alisaif

ISP
Apr 6, 2013
418
AE
Hi

Can I extract the value of an amount after decimal like;

Dhs.2,010.35 result 35

and, I want to put the value in a text box whereas if the value is greater than .50 the result should be in -ve like (-.51) and if it is less than or equal to .50 it should display in +ve like (.50). It is because to get the net value to be deducted from gross value.

Thanks

Saif
 
One way to do that would be like this:

Code:
lnBaseAmount = 2010.35
lnDecimalAmount = lnBaseAmount - ROUND(lnBaseAmount, 0)
? lnDecimalAmount   && Displays .35

If you want 35 rather than .35, you can multiply it by 100, assuming it will always contain two digits.

To add the minus sign:

Code:
lnDecimalAmount = IIF(lnDecimalAmount > 50, lnDecimalAmount * -1, lnDecimalAmount)

You might need to convert it to a string before storing it in the text box, depending on how you want the user to see it.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 

Code:
?MOD(2010.35 , 1)
But if you number is characters you need to translate that into a numeric value

Code:
?MOD(val(chrtran('2,210.35',',','')),1)

Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
ReFox XI (www.mcrgsoftware.com)
 
And yet a 3rd way would be to subtract the Integer value from the Original value.

Code:
lnBaseAmount = 2010.35
lnNewValue = lnBaseAmount - INT(lnBaseAmount)
* --- And then if you needed it this way ---
lnNewValue = (lnNewValue * 100)

Good Luck,
JRB-Bldr

 
With the inintial value being the string "Dhs.2,010.35", I'd do:

Code:
lcText = "Dhs.2,010.35"
* uncomment to test other values
* lcText = "Dhs.2,010.50"
* lcText = "Dhs.2,010.51"
ve = Right(Alltrim(lcText),2)
ve = "("+Iif(ve>"50","-.",".")+ve+")"
? ve

Bye, Olaf.
 
Or, a variation on Olaf's code, that will work with any number of decimal places (greater than zero):

Code:
lcText = "Dhs.2,010.35"
* uncomment to test other values
* lcText = "Dhs.2,010.501"
* lcText = "Dhs.2,010.5122"
* lcText = "Dhs.2,010.5"
[b]ve = ALLTRIM(GETWORDNUM(lcText, GETWORDCOUNT(lcText, "."), "."))[/b]
ve = "("+Iif(ve>"50","-.",".")+ve+")"
? ve

(I can see that this thread is going to run and run.)

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Yes, Mike. Another long running thread. But that's not bad, isn't it? Always good to see many solutions.

Code:
ve = StrExtract(lcText,".","",Occurs(".",lcText))

This would also extract any number of digits after the last "." in the string. But indeed I think Right(lcText,2) is more conform to the "keep it simple" principle. If texts have prices in them, you can normally assume two digits as the last two chars and Alltrim() already is a bonus, removing any further trailing whitespace after these fractions of the price. I know in ERP/merchandise/commodity management, in contracts you might have prices down to three or more digits, that's right.

Taking string parts of course removes the risc and failure of inexact numeric conversion and rounding errors you can have even with the round function itself. I also like the MOD 1 solution, it's my favorite among the numeric solutions and would also keep any number of decimal places.

Bye, Olaf.
 
if the variable is numeric only,

would this work? (not tested)
lnValue = 1000,201.22
? juststem(transform(lnValue))

should print: 22

Ali Koumaiha
TeknoSoft Inc.
Michigan
 
Ali,

nValue = 1000,201.22

No, that's no good, because the comma will throw it out.

? juststem(transform(lnValue))

Not quite right either. You mean JUSTEXT(), not JUSTSTEM()

But the following should work:

Code:
lcValue = [b]"[/b]1000,201.22[b]"[/b]
? JUST[b]EXT[/b](lcValue)

But only with the default settings for SET POINT and SET SEPARATOR.

mIKE



__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
That is not deductable from you writing it as "Dhs. 2,010.35".
Then use mod(field,1)*100

Bye, Olaf.
 
I know he said his variable was numeric, but if it were text then this should also work:

[tt]lcOldText = ' 1000,201.22'[/tt]
[tt]lcNewText = STUFF(lcOldText, 1, AT('.', lcOldText), '')[/tt]

[tt]? lcNewText[/tt]

[tt]22[/tt]


**********

The TT option is buggy in this new version of Tec-Tips. There is supposed to be 7 leading spaces before the '1000', not one space as shown.



mmerlinn


Poor people do not hire employees. If you soak the rich, who are you going to work for?

"We've found by experience that people who are careless and sloppy writers are usually also careless and sloppy at thinking and coding. Answering questions for careless and sloppy thinkers is not rewarding." - Eric Raymond
 
Or using his original text:

[tt]lcOldText = 'Dhs.2,010.35'[/tt]
[tt]lcNewText = STUFF(lcOldText, 1, RAT('.', lcOldText), '')[/tt]

[tt]? lcNewText[/tt]

[tt]35[/tt]


mmerlinn


Poor people do not hire employees. If you soak the rich, who are you going to work for?

"We've found by experience that people who are careless and sloppy writers are usually also careless and sloppy at thinking and coding. Answering questions for careless and sloppy thinkers is not rewarding." - Eric Raymond
 
Mike, you are right, i meant to write justext() to get the extention after the dot. i was thinking justext() and i wrote juststem()



Ali Koumaiha
TeknoSoft Inc.
Michigan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top