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!

Rounding or truncation issue 1

Status
Not open for further replies.

kss444

Programmer
Sep 19, 2006
306
US
here is a sample page of my issue.
dim payment
payment = 134.42
payment = int(payment * 100)/100
Response.Write("<BR>" & payment)
'The result is 134.41??

dim payment
payment = 134.421
payment = int(payment * 100)/100
'result is 134.42

The first way is what I have and need to use, also people are uploading these values from an excel 2007 file, so at times the value could come in as a string hench the int() conversion.
Thanks for any help on this,
KS.

Ordinary Programmer
 
I'd replace this:
payment = int(payment * 100)/100
with this:
payment = Round(payment,2)
or this:
payment = Round(CDbl(payment),2)

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks but this did not work.
Here is what I've tried.
Dim payment
payment = 134.42 '134.425
Dim payment2
payment2 = 134.42 '134.425
Dim payment3
payment3 = 134.42 '134.425
Dim payment4
payment4 = "134.42" '134.425
Dim payment5
dim payment6
payment6 = 134.421
dim payment7
payment7 = 134.42
dim payment8
payment8 = 134.42

Response.Write("<BR>" & payment & " ")

payment = int(payment * 100)/100
payment2 = FormatNumber(payment2,2)
payment3 = int(FormatNumber(payment3,2))
payment4 = cint(payment * 100)/100
payment5 = cdbl(payment)
payment6 = int(payment6 * 100)/100

payment7 = Round(payment,2)
payment8 = Round(CDbl(payment),2)


Response.Write("<BR>" & payment )
Response.Write("<BR>" & payment2 )
Response.Write("<BR>" & payment3 )
Response.Write("<BR>" & payment4 & " cint")
Response.Write("<BR>" & payment5 & " cdbl")
Response.Write("<BR>" & payment6 & "")
Response.Write("<BR>" & payment7)
Response.Write("<BR>" & payment8)

Ordinary Programmer
 
Get rid of this:
payment = int(payment * 100)/100

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
sorry, but these are end users who are putting these excel files together, so sometimes you will see numbers like 134.914
and they will upload this another developer did the *100)/100 to just get 2 decimal places.

Ordinary Programmer
 
this did not work
Your posted test is meaningless as it scratch the value of the payment variable before trying my suggestion.
What about this ?
Code:
payment1 = 134.42
payment2 = "134.42"
payment3 = Round(payment1,2)
payment4 = Round(CDbl(payment2),2)

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
I guess, that you want to truncate all numbers (payments) from input to 2 decimal places... If so try to write a function like this:
Code:
num_inp [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]134[/color] 
num_out [COLOR=#804040][b]=[/b][/color] truncate[COLOR=#804040][b]([/b][/color]num_inp[COLOR=#804040][b],[/b][/color] [COLOR=#ff00ff]2[/color][COLOR=#804040][b])[/b][/color]
wscript[COLOR=#804040][b].[/b][/color]echo num_inp [COLOR=#804040][b]&[/b][/color] [COLOR=#ff00ff]" --> "[/color] [COLOR=#804040][b]&[/b][/color] num_out

num_inp [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]134.42[/color] 
num_out [COLOR=#804040][b]=[/b][/color] truncate[COLOR=#804040][b]([/b][/color]num_inp[COLOR=#804040][b],[/b][/color] [COLOR=#ff00ff]2[/color][COLOR=#804040][b])[/b][/color]
wscript[COLOR=#804040][b].[/b][/color]echo num_inp [COLOR=#804040][b]&[/b][/color] [COLOR=#ff00ff]" --> "[/color] [COLOR=#804040][b]&[/b][/color] num_out

num_inp [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]134.421[/color] 
num_out [COLOR=#804040][b]=[/b][/color] truncate[COLOR=#804040][b]([/b][/color]num_inp[COLOR=#804040][b],[/b][/color] [COLOR=#ff00ff]2[/color][COLOR=#804040][b])[/b][/color]
wscript[COLOR=#804040][b].[/b][/color]echo num_inp [COLOR=#804040][b]&[/b][/color] [COLOR=#ff00ff]" --> "[/color] [COLOR=#804040][b]&[/b][/color] num_out

num_inp [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]134.426[/color] 
num_out [COLOR=#804040][b]=[/b][/color] truncate[COLOR=#804040][b]([/b][/color]num_inp[COLOR=#804040][b],[/b][/color] [COLOR=#ff00ff]2[/color][COLOR=#804040][b])[/b][/color]
wscript[COLOR=#804040][b].[/b][/color]echo num_inp [COLOR=#804040][b]&[/b][/color] [COLOR=#ff00ff]" --> "[/color] [COLOR=#804040][b]&[/b][/color] num_out

num_inp [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]134.426795[/color]
num_out [COLOR=#804040][b]=[/b][/color] truncate[COLOR=#804040][b]([/b][/color]num_inp[COLOR=#804040][b],[/b][/color] [COLOR=#ff00ff]2[/color][COLOR=#804040][b])[/b][/color]
wscript[COLOR=#804040][b].[/b][/color]echo num_inp [COLOR=#804040][b]&[/b][/color] [COLOR=#ff00ff]" --> "[/color] [COLOR=#804040][b]&[/b][/color] num_out

[COLOR=#0000ff]'----------------------------------------[/color]
[COLOR=#804040][b]function[/b][/color] truncate[COLOR=#804040][b]([/b][/color]num[COLOR=#804040][b],[/b][/color] decimals[COLOR=#804040][b])[/b][/color]
 [COLOR=#0000ff] ' convert to string[/color]
  num_str [COLOR=#804040][b]=[/b][/color] [COLOR=#008080]cstr[/color][COLOR=#804040][b]([/b][/color]num[COLOR=#804040][b])[/b][/color]
 [COLOR=#0000ff] ' compute the length of string[/color]
  length [COLOR=#804040][b]=[/b][/color] [COLOR=#804040][b]len[/b][/color][COLOR=#804040][b]([/b][/color]num_str[COLOR=#804040][b])[/b][/color]

  point_pos [COLOR=#804040][b]=[/b][/color] [COLOR=#008080]instr[/color][COLOR=#804040][b]([/b][/color]num_str[COLOR=#804040][b],[/b][/color][COLOR=#ff00ff]"."[/color][COLOR=#804040][b])[/b][/color]
  comma_pos [COLOR=#804040][b]=[/b][/color] [COLOR=#008080]instr[/color][COLOR=#804040][b]([/b][/color]num_str[COLOR=#804040][b],[/b][/color][COLOR=#ff00ff]","[/color][COLOR=#804040][b])[/b][/color]
 [COLOR=#0000ff] 'wscript.echo "point_pos = " & point_pos[/color]
 [COLOR=#0000ff] 'wscript.echo "comma_pos = " & comma_pos[/color]
  
  [COLOR=#804040][b]if[/b][/color] point_pos [COLOR=#804040][b]>[/b][/color] [COLOR=#ff00ff]0[/color] [COLOR=#804040][b]then[/b][/color]
   [COLOR=#0000ff] 'if number contains decimal point[/color]
    new_length [COLOR=#804040][b]=[/b][/color] point_pos [COLOR=#804040][b]+[/b][/color] decimals
  [COLOR=#804040][b]elseif[/b][/color] comma_pos [COLOR=#804040][b]>[/b][/color] [COLOR=#ff00ff]0[/color] [COLOR=#804040][b]then[/b][/color]
   [COLOR=#0000ff] 'if number contains decimal comma[/color]
    new_length [COLOR=#804040][b]=[/b][/color] comma_pos [COLOR=#804040][b]+[/b][/color] decimals
  [COLOR=#804040][b]else[/b][/color]
   [COLOR=#0000ff] 'if number doen't contain point nor comma[/color]
    new_length [COLOR=#804040][b]=[/b][/color] length
  [COLOR=#804040][b]end[/b][/color] [COLOR=#804040][b]if[/b][/color]

  new_num_str [COLOR=#804040][b]=[/b][/color] [COLOR=#008080]left[/color][COLOR=#804040][b]([/b][/color]num_str[COLOR=#804040][b],[/b][/color] new_length[COLOR=#804040][b])[/b][/color]

 [COLOR=#0000ff] 'convert to double[/color]
  truncate [COLOR=#804040][b]=[/b][/color] [COLOR=#008080]cdbl[/color][COLOR=#804040][b]([/b][/color]new_num_str[COLOR=#804040][b])[/b][/color]
[COLOR=#804040][b]end[/b][/color] [COLOR=#804040][b]Function[/b][/color]
Output:
Code:
134 --> 134
134,42 --> 134,42
134,421 --> 134,42
134,426 --> 134,42
134,426795 --> 134,42
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top