Java tutorial
//package com.java2s; public class Main { /** * Rounds the given number to the given number of significant digits. Based on an answer on <a * href="http://stackoverflow.com/questions/202302">Stack Overflow</a>. */ public static float roundToOneSignificantFigure(double num) { final float d = (float) Math.ceil((float) Math.log10(num < 0 ? -num : num)); final int power = 1 - (int) d; final float magnitude = (float) Math.pow(10, power); final long shifted = Math.round(num * magnitude); return shifted / magnitude; } }