Creating a Truncating Function

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I hope to truncate some values for calculations but cant seem to do it right.

Example:
0.05666 -> 0.05
0.05444 -> 0.05
5678 -> 5000
5432 -> 5000

Is there any simple way to code this function? Thanks in advance.
 
For the first two numbers - 0.05
int([MyNumber]*100)/100

For the next two numbers - 5000
int([MyNumber]/1000)*1000
 
I think you misunderstood me. I meant like writing a function to always
truncate it to the first digit, not a function for 2 different kinds of
decimals.

Say, if I have a new number 13.12445 and I call
mytruncatefunction(13.12445), it will return me a 10. The function just
removes all the values behind until the first non zero digit.

Sorry if I didn make any sense earlier.

Ofer said:
For the first two numbers - 0.05
int([MyNumber]*100)/100

For the next two numbers - 5000
int([MyNumber]/1000)*1000

--
\\// Live Long and Prosper \\//
BS"D


Airkon said:
I hope to truncate some values for calculations but cant seem to do it right.

Example:
0.05666 -> 0.05
0.05444 -> 0.05
5678 -> 5000
5432 -> 5000

Is there any simple way to code this function? Thanks in advance.
 
Dear Airkon:

The first step would be to find the scale of the number. That would be done
by taking the log 10 of the number and truncating that to an integer.

Next, raise 10 to that power and integer divide your value by that, then
multiply it back.

Wouldn't that give you the proper result? Perhaps not for negative numbers,
but I didn't see your specification for those. You'd best try it.

The log function is an inefficient way of doing this in terms of CPU, so if
you are doing a large number of these at a time, some other approach would
be good. Perhaps a case statement would be useful, or a comparable
if/then/else.

Tom Ellison


Airkon said:
I think you misunderstood me. I meant like writing a function to always
truncate it to the first digit, not a function for 2 different kinds of
decimals.

Say, if I have a new number 13.12445 and I call
mytruncatefunction(13.12445), it will return me a 10. The function just
removes all the values behind until the first non zero digit.

Sorry if I didn make any sense earlier.

Ofer said:
For the first two numbers - 0.05
int([MyNumber]*100)/100

For the next two numbers - 5000
int([MyNumber]/1000)*1000

--
\\// Live Long and Prosper \\//
BS"D


Airkon said:
I hope to truncate some values for calculations but cant seem to do it
right.

Example:
0.05666 -> 0.05
0.05444 -> 0.05
5678 -> 5000
5432 -> 5000

Is there any simple way to code this function? Thanks in advance.
 
Or maybe something less mathematical<g>:

Public Function FirstSigFig(D As Double) As Double
Dim S As String * 24
S = Format(D, "+0.0000000000000E-0;-0.0000000000000E-0")
FirstSigFig = CInt(Left(S, 2)) * 10 ^ CInt(Mid(S, 18))
End Function


Dear Airkon:

The first step would be to find the scale of the number. That would be done
by taking the log 10 of the number and truncating that to an integer.

Next, raise 10 to that power and integer divide your value by that, then
multiply it back.

Wouldn't that give you the proper result? Perhaps not for negative numbers,
but I didn't see your specification for those. You'd best try it.

The log function is an inefficient way of doing this in terms of CPU, so if
you are doing a large number of these at a time, some other approach would
be good. Perhaps a case statement would be useful, or a comparable
if/then/else.

Tom Ellison


Airkon said:
I think you misunderstood me. I meant like writing a function to always
truncate it to the first digit, not a function for 2 different kinds of
decimals.

Say, if I have a new number 13.12445 and I call
mytruncatefunction(13.12445), it will return me a 10. The function just
removes all the values behind until the first non zero digit.

Sorry if I didn make any sense earlier.

Ofer said:
For the first two numbers - 0.05
int([MyNumber]*100)/100

For the next two numbers - 5000
int([MyNumber]/1000)*1000

--
\\// Live Long and Prosper \\//
BS"D


:

I hope to truncate some values for calculations but cant seem to do it
right.

Example:
0.05666 -> 0.05
0.05444 -> 0.05
5678 -> 5000
5432 -> 5000

Is there any simple way to code this function? Thanks in advance.
 
Dear John:

Yes. I did consider an alpha-numeric approach. Yours does it very well. I
especially like the tricky part about how it handles negative exponentiation
through the use of the formatting.

What's all that Double D stuff? I don't think that kind of conversation is
appropriate for this newsgroup! : )

Tom Ellison


John Nurick said:
Or maybe something less mathematical<g>:

Public Function FirstSigFig(D As Double) As Double
Dim S As String * 24
S = Format(D, "+0.0000000000000E-0;-0.0000000000000E-0")
FirstSigFig = CInt(Left(S, 2)) * 10 ^ CInt(Mid(S, 18))
End Function


Dear Airkon:

The first step would be to find the scale of the number. That would be
done
by taking the log 10 of the number and truncating that to an integer.

Next, raise 10 to that power and integer divide your value by that, then
multiply it back.

Wouldn't that give you the proper result? Perhaps not for negative
numbers,
but I didn't see your specification for those. You'd best try it.

The log function is an inefficient way of doing this in terms of CPU, so
if
you are doing a large number of these at a time, some other approach would
be good. Perhaps a case statement would be useful, or a comparable
if/then/else.

Tom Ellison


Airkon said:
I think you misunderstood me. I meant like writing a function to always
truncate it to the first digit, not a function for 2 different kinds of
decimals.

Say, if I have a new number 13.12445 and I call
mytruncatefunction(13.12445), it will return me a 10. The function just
removes all the values behind until the first non zero digit.

Sorry if I didn make any sense earlier.

:

For the first two numbers - 0.05
int([MyNumber]*100)/100

For the next two numbers - 5000
int([MyNumber]/1000)*1000

--
\\// Live Long and Prosper \\//
BS"D


:

I hope to truncate some values for calculations but cant seem to do
it
right.

Example:
0.05666 -> 0.05
0.05444 -> 0.05
5678 -> 5000
5432 -> 5000

Is there any simple way to code this function? Thanks in advance.
 
Back
Top