How to round to 2 decimals with Python?

I am getting a lot of decimals in the output of this code (Fahrenheit to Celsius converter). My code currently looks like this: def main(): printC(formeln(typeHere())) def typeHere(): global Fahrenheit try: Fahrenheit = int(raw_input(“Hi! Enter Fahrenheit value, and get it in Celsius!\n”)) except ValueError: print “\nYour insertion was not a digit!” print “We’ve put your … Read more

Why does Math.round(0.49999999999999994) return 1?

In the following program you can see that each value slightly less than .5 is rounded down, except for 0.5. for (int i = 10; i >= 0; i–) { long l = Double.doubleToLongBits(i + 0.5); double x; do { x = Double.longBitsToDouble(l); System.out.println(x + ” rounded is ” + Math.round(x)); l–; } while (Math.round(x) … Read more

Formatting a number with exactly two decimals in JavaScript

I have this line of code which rounds my numbers to two decimal places. But I get numbers like this: 10.8, 2.4, etc. These are not my idea of two decimal places so how I can improve the following? Math.round(price*Math.pow(10,2))/Math.pow(10,2); I want numbers like 10.80, 2.40, etc. Use of jQuery is fine with me. 3Best … Read more

Show a number to two decimal places

What’s the correct way to round a PHP string to two decimal places? $number = “520”; // It’s a string from a database $formatted_number = round_to_2dp($number); echo $formatted_number; The output should be 520.00; How should the round_to_2dp() function definition be? 25 s 25 You can use number_format(): return number_format((float)$number, 2, ‘.’, ”); Example: $foo = … Read more