Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Rounding of decimal to next highest whole number

Posted on 2004-09-01
14
Medium Priority
?
1,259 Views
Last Modified: 2008-01-09
This code is on a asp.net page using VB

I am trying to round of a fraction to the next highest whole number or integer. I found the following code on Experts, but I keep getting an error:
 BC30519: Overload resolution failed because no accessible 'InStr' can be called without a narrowing conversion
the doucumentation says:To correct this error Specify Option Strict Off.  how do I do this?

Is there better code?
my code:  
example: intshipunit will equal 1.15 in this test, I need it to change to 2

 dim intshipunit as decimal = strtotalweight / 20
Dim x As single
Dim y() As String
Dim z As single

x = intshipunit
If InStr(1, x, ".") Then
y() = Split(x, ".")
z = x - y(0)
x = x + (1 - z)
response.write (x)
End If


0
Comment
Question by:dplsr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
  • +2
14 Comments
 
LVL 17

Expert Comment

by:AerosSaga
ID: 11957745
In the IDE Project->Properties->build
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 11957753
there you will see option explicit, option strict, and option compare you can change these settings from here.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:dplsr
ID: 11957869
In the IDE Project->Properties->build

Is this something in Visual Studio?  I am not using visual studio if it is. Can i do this right on the page or in the sub?
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 11957894
You can do this in the web.config file then:

<compilation> attribute

The <compilation> tag includes the following attributes:

    * defaultLanguage="C#|VB|C++" : Contains a list of language names to be used in dynamic compilation files.
    * debug="true|false" : Determines whether to compile in retail binaries or in debug binaries.
    * explicit="true|false" : Determines the setting of the Microsoft Visual Basic explicit compile option.
    * batch="true|false" : Determines whether batching is supported or not.
    * batchTimeout="number" : Determines the time-out period for batch compilation.
    * numRecompilesBeforeApprestart="number" : Determines the number of dynamic recompiles of resources that can happen before the application restarts.
    * strict="true|false" : Determines the setting of the Visual Basic strict compile option.

The code in Listing 1.6 shows compilation configuration settings:

<configuration>
   <system.web>
            <compilation defaultLanguage="C#"
                  debug="true"
                  numRecompilesBeforeAppRestart="10"
                  batchTimeout="10"
                  explicit="true"
                  strict="true"
                  batch="true"
            </compilation>
   </system.web>
</configuration>

http://www.codenotes.com/articles/articleAction.aspx?articleID=333
0
 

Author Comment

by:dplsr
ID: 11958022
I set strict="false" I still get the same error
0
 
LVL 33

Accepted Solution

by:
raterus earned 2000 total points
ID: 11958052
Not to question the original poster of that code, but that is a horrid way to round up!

Use this instead: System.Math.Ceiling(intshipunit)

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemmathclassceilingtopic.asp

0
 
LVL 10

Expert Comment

by:jnhorst
ID: 11958176
raterus is right (say that 10 times fast!).

InStr is old school VB and is replaced by IndexOf() in .NET, but for rounding up as you want to do, raterus' code is the way to do it.

John
0
 

Author Comment

by:dplsr
ID: 11958211
Hi raterus,

this looks better than what I had, but being a semi-newbie I  to VB I can't figure out how to apply it to

intshipunit
in
dim intshipunit as decimal = strtotalweight / 20

this is all used to set shipping charges on a web site
strtotalweight will vary per order, per number of items
0
 
LVL 24

Expert Comment

by:Justin_W
ID: 11958257
System.Math.Ceiling(intshipunit) will only work if the number is non-negative.  A more general solution is:
   System.Math.Max(System.Math.Ceiling(intshipunit), System.Math.Floor(intshipunit))
0
 
LVL 24

Expert Comment

by:Justin_W
ID: 11958270
I.e.:
   intshipunit = System.Math.Max(System.Math.Ceiling(intshipunit), System.Math.Floor(intshipunit))
0
 

Author Comment

by:dplsr
ID: 11958411
justin i should have split the points on that sorry! I did not notice the name changed!
0
 
LVL 24

Expert Comment

by:Justin_W
ID: 11958700
That's OK.  I just remembered that Math.Ceiling is fine on its own unless you need the nearest whole number with the greatest absolute value.
0
 

Author Comment

by:dplsr
ID: 11958917
What I need is the next highest whole number. Like 1.15  changed to 2 . so far this works great
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question