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!

asp formatting calculations

Status
Not open for further replies.

ifntech

Programmer
Nov 4, 2004
80
0
0
US
Using some of dreamweaver, part custom.
I'm trying to calc a date range and when I go to format and truncate it I get an error type mismatch formatnumber.

If I run it without the format it works.

<% = ((now() - (test.fields.item("date").value))/30) %>

This works outputs 10.28382726123

This is what I was trying.
<% months = ((now() - (test.fields.item("date").value))/30) %>


<% =formatnumber((months), 0,-2, -2, -2) %>

I just want 10.

thanks,
 
If months is not a number, then you would get a type mismatch. I would recommend first explicitly setting months to a number and then formatting it. Something like this (you may have to tweak it):
Code:
<% =formatnumber(clng(months), 0,-2, -2, -2)) %>

------------------------------------------------------------------------------------------------------------------------
"I am not young enough to know everything."
Oscar Wilde (1854-1900)
 
I get this now

Error Type:
Microsoft VBScript runtime (0x800A005E)
Invalid use of Null: 'clng'

<% =formatnumber(clng(months), 0,-2, -2, -2) %>
 
Code:
<%
months = 10.28382726123
months = formatnumber(clng(months),0)

response.write months
%>
 
This means that your months variable is null. You will need to check for that before you attempt to format the value.

------------------------------------------------------------------------------------------------------------------------
"I am not young enough to know everything."
Oscar Wilde (1854-1900)
 
this how you can check
Code:
months=trim("" & months)

if months<>"" then
months = formatnumber(clng(months),0)
response.write months
else
response.write "No months available"
end if
 
steven290,

That will only check if the variable months is an empty string, not if it is null. To do that, s/he would need to use the isnull() function.

------------------------------------------------------------------------------------------------------------------------
"I am not young enough to know everything."
Oscar Wilde (1854-1900)
 
actually this part

Code:
months=trim("" & months)

makes a null seem empty (null + empty = empty)
 
Ahhhh...

Then I have learned something new and my apologies for attempting to correct you... [thumbsup]

------------------------------------------------------------------------------------------------------------------------
"I am not young enough to know everything."
Oscar Wilde (1854-1900)
 
just glad i can contribute to your mental growth, this is the way i learned (and still am learning);-)
 
your code works but the issue was when there is a null value which can be solved with

months=trim("" & months)

months = formatnumber(clng(months),0)
response.write months
 
i understand that, bear w/ me

why would <% = ((now() - (test.fields.item("date").value))/30) %>

work to get 10.28382726123

but assigning to a var months doesn't?

i simply saw too many paremeters <% =formatnumber(clng(months), 0,-2, -2, -2) %>

and changed accordingly..

months = formatnumber(clng(months),0)

thanks
 
so? it wasn't actually a null...you provided a code to catch a null..yes? an extra precaution..so technically both solutions work...w/ your addition makes it prtoected from nulls...yes?

lol...just curious here

thanks steven
 
sorry typo...so, it wasn't a null issue as presummed...it was a formatnumber issue...and having your addtional code (which is an excellent choice btw) will prevent the error to come up if null...true or false?

thanks
 
k and yes the little snippet will prevent null value errors. (for anything when - search db fields, etc)
 
cool...just wanted to make sure i didn't overlook the fact it may have been a null issue when i posted my code, because it was headed in that direction, and i didn't want ifntech to get confused; otherwise, i too would have assumed the month was null w/ that error..but i saw the his original output produced a result..it wasn't a null, but a formatting issue....thanks for the clarifiaction as always;-

Brian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top