# JS fractional end-term binary method to achieve

2010-06-02  来源：本站原创  分类：Web  人气：201

Encountered a problem today, you need to decimal's last place in accordance with the following rules to carry:

Place, the rules

When the end-term in the 0-4, the end-term into 0.5. If 1.3 ==> 1.5
When the end-term in the 5-9, the end-term into 1.0. If 1.5 ==> 2.0

```<script>
function fix(d) {
//  A decimal number to an integer  .
var len = 0;
while (parseInt(d) != d) {
d = d * 10;
len++;
}

//  Remove trailing
var last = Math.abs(len == 0 ? 0 : d % 10);
var d1 = d - last;
if (last >= 0 && last < 5) {
d1 = d1 + 5;
} else {
d1 = d1 + 10;
}

//  Restore to decimal
while (len > 0) {
d1 = d1 / 10;
len--;
}

return d1;
}

var test = [-12.365, 198.8, 6, 1.2, 1.4, 1.5, 1.38, 1.70, 3.156];
for (var i = 0; i < test.length; i++) {
document.write(test[i] + " ==&gt; " + fix(test[i]) + "<br/>");
}
</script>
```

Run Results

-12.365 ==> -12.36
198.8 ==> 199
6 ==> 11
1.2 ==> 1.5
1.4 ==> 1.5
1.5 ==> 2
1.38 ==> 1.4
1.7 ==> 2
3.156 ==> 3.16

Of course, js accuracy is limited, such as 3.1561 ==> 3.1561000000000012.

If you use the Java implementation, then natural to consider using the BigDecimal implementation, otherwise, if the number of decimal places too long, sufficient deposit.

Ask js maintain the accuracy of the method.

• JS fractional end-term binary method to achieve 2010-06-02

Encountered a problem today, you need to decimal's last place in accordance with the following rules to carry: Place, the rules When the end-term in the 0-4, the end-term into 0.5. If 1.3 ==> 1.5 When the end-term in the 5-9, the end-term into 1.0. I

• Web page refresh using javascript method to achieve 2010-03-25

Web page refresh using javascript method to achieve javascript refresh page Javascript refresh the page several ways: 1 history.go (0) 2 location.reload () 3 location = location 4 location.assign (location) 5 document.execCommand ('Refresh') 6 window

• js indexof use _JavaScript indexof method 2010-08-21

js indexof usage, JavaScript indexof method indexOf () definition and usage indexOf () method returns a string value specified in the string for the first time to appear. Grammar stringObject.indexOf(searchvalue,fromindex) Parameter Description searc

• Besides full-screen method to achieve Android 2010-10-09

The method to achieve full-screen Android Method to achieve full-screen online a lot has been said, is achieved through API calls: 1, hide the status bar, in the Activity of onCreate method calls the following API: // This method requires the setCont

• The method to achieve full-screen Android 2010-10-10

The method to achieve full-screen Android Android full inside the method can be used to achieve one of two ways: 1. OnCreate in the Activity of the method calls the following API: / / This method needs to setContentView (R.layout .....) method is cal

• Background gradient method to achieve 2010-12-06

After several days of exploration, finally found the pictures do not use the method to achieve the background gradient First create the file drawable / shape.xml <? Xml version = "1.0" encoding = "utf-8"?> <shape xmlns:android

• Detailed analysis with Squid reverse proxy method to achieve 2011-05-06

from http://tech.ccidnet.com/art/737/20070417/1063859_1.html Detailed analysis with Squid reverse proxy method to achieve Posted: 2007.04.18 06:13 Source: SAN technology community Author: forgiven A proxy server is a very common use will be accessing

• A simple binary search method to achieve 2010-10-20

Binary search algorithm is to use a classic example of divide and conquer strategy. The basic idea: The less the same n elements into two halves, take a [n / 2] and x for comparison. If x == a [n / 2], is found, the algorithm terminates. If a [n / 2]

• JS refresh iframe method to achieve 2009-11-19

<iframe src="1.htm" name="ifrmname"> </ iframe> Program I: The name attribute with iframe positioning <input type = "button" name = "Button" value = "Button" onclick = "document.frames

• js method to achieve repalceAll 2010-04-16

desc = "dd ggg ffff"; String.prototype.replaceAll = function(s1,s2){ return this.replace(new RegExp(s1,"gm"),s2); alert(desc.replaceAll(' ','')); }

• JS Date object in getYear () method and getFullYear () method of difference 2010-05-14

getYear () Use getYear () function is intended for the year to 2010, for example, such as: var nowd = new Date(); var yf = nowd.getYear(); In IE, it is correct for the year: 2010, but in FF and other browsers was: 110. Reason is the FF and other brow

• . Net method to achieve RSS 2010-06-01

Quote How to. Net realized dynamic site RSS do? The main idea is to prepare a format to automatically generate RSS xml document according to universal categories. Concrete steps are as follows: Step 1: Create RSS General category using System; using

• js coverage provided the original method of rewriting method 2010-08-03

<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <script> function myblur () { alert("1. Leave "); } function bindEvent(func){ myblur = func; } function myonclick()

• js version of the md5 encryption to achieve change 2010-08-12

Call js function hex_md5 ("abc") verified by Reference: http://cssrainbow.cn/tutorials/javascript/391.html http://www.supidea.com/post/js-code-to-md5.aspx

• JS regular expression (3) - replace () method use 2010-12-16

var str = "give me some sunshine"; / / Alert (str.replace ("sunshine", "rain")); / / replace the sunshine into rain, replace method takes two parameters, the first for the source parameters, the second replacement for the new

• CSS and JS to determine whether the method is loaded 2011-05-30

Simple and effective idea: three steps First, display the default prompt: identification directly in the pages of a friendly tips: for example, xxx, the system load is not completed, we recommend that you refresh after the operation yy Second, css is

• oracle method to achieve saveOrUpdate 2011-09-05

In oracle 9i and later, provides a new function merge into. Through it, we can achieve a similar way to the hibernate saveOrUpdate. Is carried out in oracle 10G enhanced refer http://tomszrp.itpub.net/post/11835/263865 First introduced to merge into

• JS can enter the drop-down box to achieve 2009-10-16

<script language="javascript"> function changeF() { document.getElementById('makeupCo').value= document.getElementById('makeupCoSe').options[document.getElementById('makeupCoSe').selectedIndex].value; } </script> <span> <sel

• MySQL string split method to achieve 2010-06-07

For example now have a string: 1,2,44,5,666,29232 It should be split into in accordance with commas: 1 2 44 5 666 29232 But also the total number of requests it. This I wrote before. But today's standard look. 1, the specific function SQL code DELIMI

• java sorting method to achieve 2010-06-09

1. Bubble sort is the basic concept of bubble sort: comparison between two consecutive numbers in turn, will put decimal in front of large numbers on the back. Comparison of the first paragraph number 1 and 2, the decimal place before, after the rele

1
2
3
4
5
6
7
8
9
10