Rounding in pure bash
Keep in mind that I've only started looking at bash this week. All of my previous attempts were the standard find|grep foo sort of thing. =)
Once again I decided to tackle everyday functionality which ought to exist out of the box in bash but was intentionally omitted for some reason. Once again, this proves that bash isn't a real programming language. =) ---- The logic for this code was taken from http://en.wikipedia.org/wiki/Roundin...to-even_method The method is "Round-to-even", aka unbiased rounding, convergent rounding, statistician's rounding, Dutch rounding, Gaussian rounding, or bankers' rounding. I was very strict in following the logic given in the Wikipedia article. This means that I intentionally avoided certain programming structures/styles/optimizations. It also seems to make the code easier to read, adapt or change. I added in additional error-checking, and I decided to do a lot more work to deal with some technically-valid but unusual cases. I also ended up doing another little test function. I do believe there are some bash tools which can do this sort of thing, but I have no intention of continuing with bash much further. This was a fun but really strange experience. As an aside, this code also happens to append additional zeros if it needs to. Tests: Code:
Wikipedia cases Code:
replace_character() { |
Nice. However, what about something like this...?
Code:
printf "%4.2f\n" 3.04501 Code:
for i in 3.016 3.013 3.015 3.045 3.04501 |
Quote:
For reference for anyone else who was in that position, this is a good site: - ou800.doc.sco.com (format modifiers) I do notice that printf is part of awk/gawk. I wanted to make a bash-only solution. I didn't realise printf was an external command. I guess I should stick with echo -n from now on. Quote:
This was more of a learning experience anyway.. I'm not going to put the code to significant (if any) use. I wouldn't want to be pedantic and implement all the variations, even though that would be a simple side project. |
Quote:
Code:
man bashbuiltins Code:
$ type echo Cheers! :) |
No joy on man bashbuiltins, but man builtins does give me the list.
You're right, printf is a builtin for me! Well that's hilarious. I really did hack together some legacy crap that's been replaced by a oneliner. =) But I guess that's just the way it is. The included man page "documentation" (used loosely) as well as the official documentation as well as the best tutorials and blog postings didn't mention rounding. Just like my grandpa could say "back in my day we had to read the source code, and all the comments were misleading too", one day I could tell my grandkids "back in my day the documentation was written by the *programmer*, and all their examples sucked". Community documentation ftw. I'm so glad wikis are getting popular for online program documentation, and even more glad that MediaWiki gets chosen more ThanThoseOtherCrappyWikis so I can actually help. Urr, ok.. ranting concluded. ---- For reference, man builtins: Code:
NAME |
All times are GMT -5. The time now is 11:12 PM. |