Jump to content

Excel Rounding Issue


Recommended Posts

I'm trying to get a number with (at most) a two digit decimal from an Excel document. The document contains decimals with various lengths. It would seem that using Round() would solve all of my issues, however, it will occasionally round down instead of up. 

This only happens in unusual circumstances that I have yet to be able to make reproduce-able, unfortunately. From what I've gathered from the author of the document, the value is calculated from a formula that is then copied to the final workbook.

When I open up the Excel document (rename as .zip, extract, and inspect \xl\worksheets\[sheet].xml), I found that instead of storing the value as 704,929.695 it stores the value as 704,929.69499999983 internally. I would be okay with this, except that AutoIt prints the value (to the console) as 704,929.695. Excel will round the value in formulas up to 704,929.70 because the value is probably supposed to be 704,929.695?

I found a sort of fix with this:

Func FixRound($vNum, $iDec = Default)
    Return Round(StringStripWS($vNum & " ", 8), $iDec)
EndFunc

Is this an issue related to floating point numbers? Is there a way to see the actual value inside Excel inside AutoIt? Instead of getting the .Value of a cell, I could try using .Text, but this will cause more issues than it will fix, I think

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

Link to comment
Share on other sites

Excel uses its own rules!

Local $v = 704929.69499999983
ConsoleWrite(StringFormat("%.11f", $v) & @LF)
ConsoleWrite($v & @LF)
ConsoleWrite(StringFormat("%.2f", $v) & @LF)
$v = 704929.695
ConsoleWrite(StringFormat("%.11f", $v) & @LF)
ConsoleWrite($v & @LF)
ConsoleWrite(StringFormat("%.2f", $v) & @LF)

 

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

I don't think I understand. That doesn't round for me... it just truncates

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

Link to comment
Share on other sites

Local $v = 704929.69499999983
ConsoleWrite(StringFormat("%.11f", $v) & @LF)   ; 704929.69499999983
ConsoleWrite($v & @LF)                          ; 704929.695
ConsoleWrite(StringFormat("%.2f", $v) & @LF)    ; 704929.69
$v = 704929.695
ConsoleWrite(StringFormat("%.11f", $v) & @LF)   ; 704929.69499999995
ConsoleWrite($v & @LF)                          ; 704929.695
ConsoleWrite(StringFormat("%.2f", $v) & @LF)    ; 704929.69

ConsoleWrite(Round($v, 2) & @LF)                ; 704929.7
ConsoleWrite(StringFormat("%.2f", Round($v, 2)) & @LF)  ; 704929.70

Like most real numbers, 704929.695 can't be represented exactly in a double. AutoIt rounds the value correctly when asked to.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

But that is the weird thing. When Excel stores the value internally as 704929.69499999983 but displays .695 and I read the value from Excel, AutoIt prints the value as .695 but rounds it down like it is .6949

Try this code with the Excel document attached...

#include <Excel.au3>

Main()

Func Main()

    Local $oBook = _Excel_BookAttach("Example.xlsx", "filename")
    If @error Then Exit ConsoleWrite("Can't attach to workbook -- Open it" & @LF)

    Local $vVal = _Excel_RangeRead($oBook, 1, "B1")
    If @error Then Exit ConsoleWrite("Can't read Excel range" & @LF)

    ConsoleWrite($vVal & @LF)                   ; 704929.695
    ConsoleWrite(Round($vVal, 2) & @LF)         ; 704929.69  <- Should Round up?
    ConsoleWrite(Round($vVal & " ", 2) & @LF)   ; 704929.7

EndFunc

 

Example.xlsx

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

Link to comment
Share on other sites

Yes there is some kind of a glitch inside excel, if you rewrite B1 manually then it is working for me (even just doing a del key at the end of the field followed by  a Enter key).

or

If you do $vVal = round($vVal,3) after reading, it is also working.

The problem seems to come on how you created B1, by copying, or by an equation, or other means ?  But the problem lies there, as it seems there is more decimal that it is actually showing...

Edited by Nine
Link to comment
Share on other sites

Yes, I think the value was creating with a formula that was then copy/paste (values) into another workbook. I think at this point, my best option might be to use Excel to round the values as it properly rounds the value up to .70 even if the value is stored strangely. Something like setting the offset cell to =Round(B1,2) and reading that instead.

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

Link to comment
Share on other sites

not directly related, but i remember some issues rounding with VBA functions:

Round() and Application.Round use different rounding algorithms, as explained here:

http://www.vbaexpress.com/forum/showthread.php?6889-Solved-Is-It-quot-Round-quot-or-quot-Application-Round-quot

https://stackoverflow.com/questions/265926/round-function-in-excel-worksheet-function-vs-vba

Link to comment
Share on other sites

Thanks @robertocm! I never knew that VBA uses Banker's rounding... which is apparently applicable in accounting, though I've never heard anyone mention that here 😐 I was thinking about using Application.Round to fix the issue too, but I guess that won't work either 🤦‍♂️

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

Link to comment
Share on other sites

Things would be much simpler if everything used the least damaging rounding method know as half to even see https://en.wikipedia.org/wiki/Rounding#Round_half_to_even yet the whole article is worth reading carefully.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

The problem that I face with rounding is that I'm processing journal entries where a whole penny is the smallest unit, but they're submitted to me with various lengths. Usually, rounding to the nearest whole penny works, but occasionally they don't balance to 0. I'd love to say I have a fix for this, but I honestly just throw it back to the user to fix and restart.

@Subz Reading the formula instead of the value worked perfectly, thank you! I'll need to copy/paste values beforehand, but that's a good idea anyways :)

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...