Floating point number! By 100, 10 after the accuracy change!

Source: Internet
Author: User

First posted a URL, feel written in very detailed, it seems that the principle of analysis, but did not see how, as if not to say how to solve ...

http://justjavac.iteye.com/blog/1864938

Phenomenon:

0.56 *  - //56.00000000000001 0.56 * Ten //5.6000000000000005 0.57 * Ten //5.699999999999999 5.6 / Ten //0.5599999999999999

This is true for all browsers including IE6

Scene appears

Need to show 0.56 as 56%, then incredibly 56%, no words.

Solve:

Seems normal:

0.57*1000/10

But the problem remains:

0.5757 *  + / Ten //57.57000000000001

The reasons are:

575.7 / Ten //reason is 5.7 except ten

Rule of discovery: avoid 0.56 or two decimal places near 5.6 to 100 or 10 multiplication

so: Multiply the number one bit more than the decimal, and then remove the extra multiples.

0.5757 * 100000 /  + //Normal

But this kind of scheme, does not rely on the principle, relies on most intuition, therefore, on the hard recruit:

Turn the string first, and then move the decimal point. But this kind of scheme is less efficient ...

function topercent(Num) {    varArr= (Num+ "').Split('. '),Otherhalf= (Arr[1] ?Arr[1] : "') + ' a ';Otherhalf=Otherhalf.substr(0, 2) + '. ' +Otherhalf.substr(2);    return (Arr[0] +Otherhalf) * 1;}topercent(. $) ///topercent(5.7) //570topercent( $) //5700

Floating point number! By 100, 10 after the accuracy change!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.