Javascript运算精度问题,建议先转换成整数再计算

解决方案 »

  1.   

    Why is 239.40*100 = 29339.999999999996 in Javascript ? 
    http://support.intel.com/support/processors/pentium/fdiv/Trying it on several platforms (IE 4.01/5.01, Win 95/98/NT; NS 4.7, Win 
    95/98/NT) it produces 23940 as expected.Javascript, like almost all other programming languages, uses native 
    'floating-point' routines at some point to do mathematical calculations. 
    These native routines are generally perfomed either by the CPU or a 
    math-coprocessing chip.There are always rounding errors associated with floating-point 
    calculations due to the fact that all numeric values have to be stored 
    in some fixed number of bits. So expressions such as 
    2.71894734524524523466451945 may be rounded to fewer decimal places 
    internally. In this case, the above multiplied by 1000 could result in 
    2178.954734525 instead of 2718.94734524524523466451945, depending on the 
    precision of the CPU.Your equation, however, should be easily handled by any modern hardware 
    processor. Chances are you have either a browser with a very bad 
    Javascript engine, or a computer with a defective processor. A few years 
    ago, Intel released some PII processors that where found to have a 
    defect in performing floating point operations, producing the kind of 
    error you see. Check the above URL for help if you think this may be the 
    case.http://support.intel.com/support/processors/pentium/fdiv/
    的内容如下:Pentium Processors
    FDIV Replacement Program Information PLEASE NOTE:This product is no longer being manufactured by Intel. THESE DOCUMENTS ARE PROVIDED FOR HISTORICAL REFERENCE PURPOSES ONLY AND ARE SUBJECT TO THE TERMS SET FORTH IN THE "LEGAL INFORMATION" LINK BELOW. For information on currently available Intel products, please see www.intel.com and/or developer.intel.com  In December of 1994, Intel announced a lifetime replacement policy on the well publicized floating point unit flaw, contained in the then current version (60-100MHz) of the Pentium? processor. Non-FPU flawed versions of the Pentium processor began shipping in late 1994. If you took delivery of your system on Jan 1, 1995 or later, it is increasingly probable that you have a processor without the flaw. Therefore, it is important that you check your system to determine if a replacement is needed. See question 2 in the FAQs to find out how you can easily check your system for the flaw.
    The replacement program is directed at End Users of working systems who are concerned about the impact of this flaw on their applications (see White Paper for more information). The FAQ describes the replacement process as well as answers commonly asked questions. In addition to this guide, you can call the customer service numbers for information regarding the replacement program. Frequently Asked Questions (updated - 14 May 98) about the Pentium? processor replacement program 
    Intel? Processor Frequency ID Utility: a utility that will identify the Intel processor contained in your PC 
    Customer Service Telephone Numbers for replacement 
    Intel White Paper: Detailed Statistical Analysis of Floating Point Flaw in Pentium Processors