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

another regex needed 1

Status
Not open for further replies.

kss444

Programmer
Sep 19, 2006
306
US
Hello all,

I am needing a regex script/pattern to format a number to 2 decimal places so:
123.44 will be 123.44
123.445 will be 123.44
I do not want rounding of any type.
Thanks,
KS

Ordinary Programmer
 
Code:
[COLOR=green]'[URL unfurl="true"]http://msdn.microsoft.com/en-us/library/ms974570.aspx[/URL]
'[URL unfurl="true"]http://msdn.microsoft.com/en-us/library/yab2dx62.aspx[/URL]
'[URL unfurl="true"]http://www.regular-expressions.info/index.html[/URL][/color]
set re = new regexp
dim strInput

dim Matches
dim Match

strInput = "123.456"

re.Pattern = "(^\d*\.\d{2})"
re.IgnoreCase = true
wscript.echo(strInput)
if re.Test(strInput) then
	wscript.echo(re.Execute(strInput)(0).SubMatches(0))
end if
 
I ended up using instead.

objRegExp.Pattern = "^[-+]?\d+(\.\d{2})?"
Set myMatches = objRegExp.Execute(strtoCur)
strCurFormat = myMatches.Item(0)



Ordinary Programmer
 
Why not simply this ?
strCurFormat = Fix(100*strtoCur)/100

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
One more thing if someone can help.
I am using this pattern ^[-+]?\d*(\.\d{1,2})?
now I need to account for a ,. ex. 3,000

Thanks again everyone.

Ordinary Programmer
 
I'd try this:
strCurFormat = Fix(100*CDbl(strtoCur))/100

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
strCurFormat = Fix(100*CDbl(strtoCur))/100 does not work it takes my value 134.42 and converts it to 134.41.

That value is coming from an excel file the cell is formated as a number, but the format can be anything. These are users who are just doing cut and paste and importing files, so we have no control and have to program around everything.

I think that the regex is the best way so far, I just can't find one that will take the following
1234.12 = 1234.12
1234.1234 = 1234.12
1,234.12 = 1,234.12
Currency formats and truncate the decimals to 2 places without rounding.

the only thing I found that works so far is:
Dim objRegExp, outputStr, myMatches
Set objRegExp = New Regexp
'LogEvent "val= " & strtoCur, false, false, false
objRegExp.IgnoreCase = True
objRegExp.Global = True
objRegExp.Pattern = "^[\s-+]?\d*(\.\d{1,2})?"

Set myMatches = objRegExp.Execute(strtoCur)
strCurFormat = myMatches.Item(0)
Now I just need it to handle 1,234 and 1234. , optional

Ordinary Programmer
 
Also this is vbscript in a classic asp page.

Ordinary Programmer
 
And this ?
strCurFormat = Fix(100*CCur(strtoCur))/100

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Sorry PHV, but I have tried all those formats FormatNumber, CDBL, int, CCur but it rounds down by 1 penny.


But I think this will work.
Dim objRegExp, outputStr, myMatches
Set objRegExp = New Regexp
'LogEvent "val= " & strtoCur, false, false, false
objRegExp.IgnoreCase = True
objRegExp.Global = True
objRegExp.Pattern = "^[\s+-]?([0-9]\d*\d{0,2}(,?\d{3})*)(\.\d{1,2})?"

Set myMatches = objRegExp.Execute(strtoCur)
strCurFormat = myMatches.Item(0)

Ordinary Programmer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top