How to round up a number to nearest 10?
floor()
will go down.
ceil()
will go up.
round()
will go to nearest by default.
Divide by 10, do the ceil, then multiply by 10 to reduce the significant digits.
$number = ceil($input / 10) * 10;
Edit: I've been doing it this way for so long.. but TallGreenTree's answer is cleaner.
How to round an integer up or down to the nearest 10 using Javascript
Divide the number by 10
, round the result and multiply it with 10
again, for example:
33 / 10 = 3.3
3.3 rounded = 3
3 × 10 = 30
console.log(Math.round(prompt('Enter a number', 33) / 10) * 10);
How to round up to nearest 10 or 100 or x
Please try this formula
System.out.println(Math.ceil(x*Math.pow(10, -Math.floor(Math.log10(x)))) / Math.pow(10, -Math.floor(Math.log10(x))) ) ;
You can try this formula online.
class Main {
public static void niceround(double x) {
System.out.println(x+" to " + Math.ceil(x * Math.pow(10, -Math.floor(Math.log10(x)))) / Math.pow(10, -Math.floor(Math.log10(x))));
}
public static void main(String[] args) {
niceround(0.0322);
niceround(3.22);
niceround(32.2);
niceround(42.2);
niceround(422.2);
}
}
test
/usr/lib/jvm/java-1.8.0-openjdk-amd64/bin/java -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:36808,suspend=y,server=n -Dfile.encoding=UTF-8 -classpath /usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/charsets.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/ext/cldrdata.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/ext/dnsns.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/ext/icedtea-sound.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/ext/jaccess.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/ext/localedata.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/ext/nashorn.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/ext/sunec.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/ext/sunjce_provider.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/ext/sunpkcs11.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/ext/zipfs.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/jce.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/jsse.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/management-agent.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/resources.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/rt.jar:/home/dac/proj/javatest2016/out/production/javatest2016:/home/dac/Downloads/idea-IU-145.972.3/lib/idea_rt.jar Main
Connected to the target VM, address: '127.0.0.1:36808', transport: 'socket'
0.0322 to 0.04
3.22 to 4.0
32.2 to 40.0
42.2 to 50.0
422.2 to 500.0
Disconnected from the target VM, address: '127.0.0.1:36808', transport: 'socket'
Process finished with exit code 0
How can I round a number down to the nearest 10?
You could use TRUNCATE function as follow:
SELECT TRUNCATE(819, -1);
SELECT TRUNCATE(812, -1);
Result:
810
Rounding a number to its nearest 10's place javascript
value = 2345;
log_10 = parseInt(Math.log(value) / Math.log(10));
basePower = Math.pow(10, log_10);
result = ((((value/basePower)).toFixed(0))* basePower);
alert(result);
How to round up number to nearest 100/1000 depending on number, in JavaScript?
You could take the logarithm of ten and round up the value for getting the value.
function roundup(v) { return Math.pow(10, Math.ceil(Math.log10(v)));}
console.log(roundup(87)); // 100console.log(roundup(776)); // 1000console.log(roundup(2333)); // 10000
Related Topics
How to Prevent My Site Page to Be Loaded Via 3Rd Party Site Frame of Iframe
Listing All the Folders Subfolders and Files in a Directory Using PHP
How to Pass Extra Variables in Url With Wordpress
MySQL Join Two Tables With Comma Separated Values
PHP Script to Loop Through All of the Files in a Directory
Is It Safe to Trust $_Server['Remote_Addr']
Why Won't My PHP App Send a 404 Error
Codeigniter Htaccess and Url Rewrite Issues
How to Loop Through a MySQL Result Set More Than Once Using the MySQL_* Functions
How to Check File Types of Uploaded Files in PHP
Architecture More Suitable For Web Apps Than MVC
Best Way to Test For a Variable'S Existence in PHP; Isset() Is Clearly Broken
What Is Function Overloading and Overriding in PHP
Create or Write/Append in Text File
Efficiently Counting the Number of Lines of a Text File. (200Mb+)
Warning "Do Not Access Superglobal $_Post Array Directly" on Netbeans 7.4 For PHP