VBScript rounding problem, any, n/a

Posted on 2007-08-07
Last Modified: 2013-11-05
Can anyone tell me why this displays as "47332" instead of "47333"?

  strPrice = "47332.95"
  dPrice = cdbl(strPrice)
  lensp = len(strPrice)-3
  xprice = left(cstr(round(dprice,2)),lensp)
Question by:dcass
    LVL 58

    Accepted Solution


    dPrice = cdbl(strPrice)
    dPrice => 47332.95
    round(dprice,2) => 47332.95
    cstr(round(dprice,2)) => cstr(47332.95) => 47332.95
    left(cstr(round(dprice,2)),lensp) => left(47332.95,5)
    xprice = left(cstr(round(dprice,2)),lensp) => 47332

    Why are you doing that? Why not just

    xprice = round(strPrice, 0)
    LVL 65

    Assisted Solution

    amit_g is right, you are rounding
    because that is
    47332.95 minus 3 characters

    Just use the alternative that amit_g suggested and it will automatically round the number to zero decimal places.
    The Round function:



    Author Comment

    Cause I didn't know what I was doing.
    Thanks, both.
    LVL 65

    Expert Comment

    No problem's all a learning process....we don't mean to come across disrepectful, we were all there once....



    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    This script will sweep a range of IP addresses (class c only, and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
    This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now