We accept safe and secure Credit Card and PayPal Payments.
 
Perl Scripts

All Count
Attachment Mailer
Perl Scripts Build A FAQ Plus
Perl Scripts Clock In Center
eBackup Automated
Easy Poll
eSurvey
Fetch a File
Form Maker
Mailing List Server
MySQL Mate
PDF Creation
QCart
Quick Fix
Quote of the day
Speed Search
Task Manager
Traffic Pack
Upload Plus
Upload Gold
Upload Pro
Website Manager

 
Free Perl Downloads

Free Perl Scripts Main Page
Free Perl Scripts 404 Alerter
AccessLog Viewer
Build A FAQ
PHP Scripts eBackup
Free PHP Scripts ErrorLog Viewer
eVars - Server Info
HT Pass Creator
Upload Lite
Website Manager

 
JavaScripts

Free Java Scripts Alert Boxes
Free JavaScripts Browser Sniffer
Check email
Generators
Slide Show
Sudoku
Window Maker
More...

 
Extra Utilities

ASP Scripts Ascii Codes
Free ASP Scripts Color Picker
Font Finder
HT Pass Creator
Meta Cloak
Meta Magic
Pano Zooms
SlideShow
Server Size

 
Online Tutorials

Free HTML Scripts Glossary
Free HTML Scripts HTML
JavaScript
MySQL
SSI
Build Traffic
Other

 
Miscellaneous

About Us
Graphics
Testimonials
Installations
Latest versions

 
Hawk Eye in Tennis

Should Hawk Eye replace linesmen and lineswomen at all tennis tournaments?







 
View all Polls

Run Polls on your site

Run your own Surveys

 
Store Download FAQs Contact Us Support Programming Policies  
JavaScripts :: Miscellaneous :: Converting Binary to Decimal

Programmatic calculation don't always yield the expected result. Especially when dealing with decimal numbers, or monetary calculations. Your initial thoughts may lead you to believe a bug exists in the language you're programing or coding in. In actual fact, it's expected behavior known as binary floating point approximation. Most modern computers process information stored as 1's and 0's, in groups of 8, known as bits, bit units, or binary digits. Humans on the other hand, process information using the decimal numbering system.

In a nutshell, the programing language converts decimal values to binary values so the Computer's processor can understand, then we need to have the binary values converted back to decimal values so humans can understand. You'd think the programing language would do this for us, but it doesn't. Good news is, you've found a solution.

Try these simple calculations in JavaScript :

<script>alert(1.02 + 1.01)</script>

Returns: 2.0300000000000002

<script>alert(3.3 * 3)</script>

Returns: 9.899999999999998

<script>alert(3 * 0.3)</script>

Returns: 0.89999999999999 

<script>alert(7 * 9.95)</script>

Returns: 69.64999999999999
Binary values only pose a problem when processing decimals. Integers or whole numbers can be correctly converted and processed. So the simple solution is to convert your decimals to whole numbers by multiplication, then divide the computed result.

Try the same computations above, with this simple decimal to integer solution applied, which cover most, not all, possibilities.

<script>alert(((1.02 * 100) + (1.01 * 100)) / 100)</script>

Returns: 2.03

<script>alert((3.3 * 100) * (3 * 100) / 10000)</script>

Returns: 9.9

<script>alert((3 * 100) * (0.3 * 100) / 10000)</script>

Returns: 0.9 

<script>alert(7 * (9.95 * 100) / 100)</script>

Returns: 69.64999999999999
Well, the last one failed to make an impact.

<script>alert(9.95 * 100)</script>

Returns: 994.9999999999999
So here's a slightly more complex script which you can use for monetary calculations. This function takes any number, regardless of decimal places, coverts it to 2 decimal places, then removes the decimal place to emulate multiplying the number by 100. Because no mathematical processing takes place, the number returned will not off by 0.01, nor will it contain an unexpected number of digits. You can then apply your formula to the figure returned and divide the product by 100 to get an accurate result.

<script>
function make100X(n){
	n = n.toString();
	
	if(n.search(/\./) >= 0){
		stringLength = n.length;
		decimalPosi  = n.search(/\./);
		decimalPosi  = stringLength - decimalPosi - 1
		
		if(decimalPosi == 0){
			n = n + "00";
		} else if(decimalPosi == 1){
			n = n + "0";
		} else if(decimalPosi > 2){
			n = n.substr(0, n.length - decimalPosi + 2);
		}
	} else {
		n = n + "00";
	}
	
	return n = n.replace(/\./g, "");
}

alert((7 * make100X(9.95) / 100));
</script>

Returns: 69.65
Of course, some products are returned with only 1 decimal place, which isn't all that great when dealing with monetary figures. So you can use the following functions to convert any figure, be it an integer or a decimal with any number of places.

<script>
function formatAmount(n){
	str = String(n);
	DECI = new Array();
	DECI = str.split(".");
	
	if(DECI[1]){
		DECI[1] = DECI[1].substr(0,2);
	} else {
		DECI[1] = "";
	}
	
	if(DECI[1].length == 1){
		DECI[1] = DECI[1] + "0";
	}
	
	if(DECI[1].length == 0){
		DECI[1] = DECI[1] + "00";
	}
	
	n = DECI[0] + "." + DECI[1];
	
	return n;
}

alert(formatAmount(80));
alert(formatAmount(80.2));
alert(formatAmount(80.2123478599999));
</script>

Returns: 

88.00
88.20
88.21
Speed Search our site
Website Monitoring with SMS TXT alerts

Mailing list management made easy

Discounted Scripts
Subscribe to our periodial newsletter to receive special offers.

Bathroom Hygiene
How often do you thoroughly clean your bathroom?








View all Polls

Run Polls just like this one on your website!


About us | Contact us | Script FAQs | Script Support | Website Hosting | Our Policies | Store | Testimonials
Subscribers log in here. Subscribe to our periodical newsletter for special offers on commercial scripts, hosting, exciting business opportunities, and industry news and events. Our Mailing List Software is powered by Consolidator    
HTML Plain text
©1999 - 2015 All content Copyright PerlScriptsJavaScripts.com Proudly hosted by LinuxHostingPlans.com