round up nearest 0.10

I need to round up to the nearest 0.10 with a minimum of 2.80

var panel; if (routeNodes.length > 0 && (panel = document.getElementById('distance'))) { panel.innerHTML = (dist/1609.344).toFixed(2) + " miles = £" + (((dist/1609.344 - 1) * 1.20) + 2.80).toFixed(2); }

any help would be appreciated

-------------Problems Reply------------

var number = 123.123;

Math.max( Math.round(number * 10) / 10, 2.8 ).toFixed(2);

If you need to round up, use Math.ceil:

Math.max( Math.ceil(number2 * 10) / 10, 2.8 )

Multiply by 10, then do your rounding, then divide by 10 again

(Math.round(12.362 * 10) / 10).toFixed(2)

Another option is:

Number(12.362.toFixed(1)).toFixed(2)

In your code:

var panel;
if (routeNodes.length > 0 && (panel = document.getElementById('distance')))
{
panel.innerHTML = Number((dist/1609.344).toFixed(1)).toFixed(2)
+ " miles = £"
+ Number((((dist/1609.344 - 1) * 1.20) + 2.80).toFixed(1)).toFixed(2);
}

To declare a minimum, use the Math.max function:

var a = 10.1, b = 2.2, c = 3.5;
alert(Math.max(a, 2.8)); // alerts 10.1 (a);
alert(Math.max(b, 2.8)); // alerts 2.8 because it is larger than b (2.2);
alert(Math.max(c, 2.8)); // alerts 3.5 (c);

var miles = dist/1609.344
miles = Math.round(miles*10)/10;
miles = miles < 2.80 ? 2.80 : miles;

to round to nearest 0.10 you can multiply by 10, then round (using Math.round), then divide by 10

Category:javascript Views:2 Time:2010-02-05
Tags: javascript

Related post

  • Rounding to nearest 100 2009-02-25

    First number needs to be rounded to nearest second number. There are many ways of doing this, but whats the best and shortest algorithm? Anyone up for a challenge :-) 1244->1200 1254->1300 123->100 178->200 1576->1600 1449->1400 123

  • round to nearest .25 javascript 2009-10-12

    Hay, i need a hand. I want to convert all numbers to the nearest .25 So... 5 becomes 5.00 2.25 becomes 2.25 4 becomes 4.00 3.5 becomes 3.50 Thanks --------------Solutions------------- Here’s an implementation of what rslite said: var number = 5.12345

  • How to round to nearest X minutes with PL/pgSQL? 2010-01-25

    How I can round to nearest X minutes? Here's my attempt: DECLARE _stamp ALIAS FOR $1; -- timestamp _nearest ALIAS FOR $2; -- minutes (integer) _minutes decimal; _ret timestamp; BEGIN _ret := date_trunc('minute', _stamp); SELECT EXTRACT (minute FROM _

  • How to round to nearest Thousand? 2011-03-01

    How Do I round a number to its nearest thousand? function round($var) { // Round it } --------------Solutions------------- PHP allows negative precisions for round such as with: $x = round ($x, -3); // Uses default mode of PHP_ROUND_HALF_UP. As a gen

  • Round the nearest number 2011-03-09

    I tried everything from round the nearest code. It is not the code that I'm looking for. I tried round the nearest number 0.00750 to this 0.0080 How do around before decimal? I want something like this: 0.0089 => 0.0090 0.0021 => 0.0020 0.1022

  • T-SQL datetime rounded to nearest minute and nearest hours with using functions 2011-07-12

    In SQL server 2008, I would like to get datetime column rounded to nearest hour and nearest minute preferably with existing functions in 2008. For this column value 2007-09-22 15:07:38.850, the output will look like: 2007-09-22 15:07 -- nearest minut

  • T-SQL: Round to nearest 15 minute interval 2009-05-06

    What's the best way to round a HH:MM value to the closest 15 minute interval? I don't track seconds so they don't matter. 00:08:00 becomes 00:15:00 00:07:00 becomes 00:00:00 01:59:00 becomes 02:00:00 and so on. Is there an elegant, non UDF or Case st

  • How to set FormCalc round to nearest quarter hour? 2009-05-29

    I have this time sheet that has four "In" and four "Out" columns,and calculates hours worked in a two week pay period. There is a column on the right that calculates the hours worked for the day (see script below). I need it to round to the nearest q

  • Round to nearest five C# 2009-10-07

    I'm using C# and I need to round a double to nearest five. I can't find a way to do it with the Math.Round function. How can I do this? What I want: 70 = 70 73.5 = 75 72 = 70 75.9 = 75 69 = 70 and so on.. Is there an easy way to do this? ------------

  • Round to nearest 5 in SQL Server 2009-11-09

    I have a Money column in my SQL Server 2008 table. In my below query how can I round it to nearest 5$ select FineAmount from tickets Thanks --------------Solutions------------- select round(FineAmount*2,-1)/2 from tickets or to put nicholaides sugges

  • Rounding to nearest number in C++ using Boost? 2010-07-18

    Is there a way to round to the nearest number in the Boost library? I mean any number, 2's, 5's, 17's and so on and so forth. Or is there another way to do it? --------------Solutions------------- int nearest = 5; int result = (input+nearest/2)/neare

  • Python - Round to nearest 05 2010-11-24

    Hvor can I en python do the following rounding: Round to the nearest 05 decimal 7,97 -> 7,95 6,72 -> 6,70 31,06 -> 31,05 36,04 -> 36,05 5,25 -> 5,25 Hope it makes sense. --------------Solutions------------- BTW 7.97 is nearer to 7.95 t

  • Round to nearest low-level implementation 2011-05-06

    I've got some low-level question: Let's assume we've got the decimal number 0.1 (we can represent this very exactly by writing 1/10 - but that is not a solution). Converted into a binary number, this gives us 0,00011 with the last 4 digits infinitely

  • In Excel, how to round to nearest fibonacci number 2011-06-28

    In Excel, I would like to round to the nearest fibonacci number. I tried something like (sorry with a french Excel): RECHERCHEH(C7;FIBO;1;VRAI) -- HLOOKUP(C7, FIBO, 1, TRUE) where FIBO is a named range (0; 0,5; 1;2;3;5;8;etc.) my problem is that this

  • javascript - how to round to nearest integer 2011-08-24

    Possible Duplicate: How can I round of to whole numbers in JavaScript? In javascript is there a function which allows me to round to the nearest integer? Either up or down. So: 2.1 = 2 2.9 = 3 4.5 = 5 1.1 = 1 --------------Solutions------------- Use

  • round to nearest nice number 2011-10-11

    I'm writing an application which requires rounding labels to the nearest 'nice' number. I'll put some code below to demonstrate this, but my issue is that I was using a series of else ifs to find this number but I cannot be sure of the upper limit so

  • R round to nearest .5 or .1 2011-12-29

    I have a data set of stock prices that have already been rounded to 2 decimal places (1234.56). I am now trying to round to a specific value which is different for each stock. Here are some examples: Current Stock Price Minimum Tick Increment Desired

  • Java rounding to nearest 0.05 2012-02-13

    I am trying to find a way to round values to the nearest 0.05. For example: 0.93 rounds to 0.95 0.81 rounds to 0.80 0.65 stays 0.65 0.68 to 0.70 0.67 to 0.65 Is there a simple way to do this in Java? --------------Solutions------------- One option fo

  • MYSQL Date Time Round To Nearest Hour 2012-03-13

    I have a date time field in a MySQL database and wish to output the result to the nearest hour. e.g. 2012-04-01 00:00:01 should read 2012-04-01 00:00:00 --------------Solutions------------- You can do it with some date arithmetic: SELECT some_columns

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.074 (s). 11 q(s)