I need to round the number of a field - javascript

I have tried many things about the rounding of a filed but I get NaN, here is what I am trying to round up to 999999,9
document.forms[0].NB_CONCN_MOY_DCO_MS
I have tried Math.round(document.forms[0].NB_CONCN_MOY_DCO_MS.value)
and
document.forms[0].Math.round(NB_CONCN_MOY_DCO_MS.value)
what can I do now.

If you want to round a number to one decimal place in JavaScript, use someNumber.toFixed(1). Note that the value of form fields is a string (not a number) so you'll want to convert it to a number first.
var n = document.forms[0].NB_CONCN_MOY_DCO_MS.value * 1;
var rounded = n.toFixed(1);
If your value has commas in it to represent decimal values, you will need to fix the string to use periods instead first:
var n = document.forms[0].NB_CONCN_MOY_DCO_MS.value.replace(/,/,',') * 1;
var rounded = n.toFixed(1);

Related

How to add decimals to a number in JavaScript?

I am currently trying to add two decimal places to the end of the number 1000 (I need it to be 1000.00)
I am trying to use: parseFloat(1000).toFixed(2) but it keeps returning a string. When I do parseFloat((1000).toFixed(2)) it returns a number, but gets rid of the decimal places. Is there a way to convert the number 1000 into the number 1000.00 without returning a string?
Try to use .toLocaleString()
var n = 1000;
var nWithZerto = n.toLocaleString("en",{useGrouping: false,minimumFractionDigits: 2});
Since, Javascript treat both integer and, decimal as Number, so it doesn't matter in calculation.
But, if you are printing it then only you require formatting and it can be done as (1000).toFixed(2)-> string.

Do not strip trailing zeros in JavaScript Decimal constructor

i have strings that represents money. E.g 29.00 or 29.10 or 29.13 the currency however can change and does not necessarily lead to a value that has two decimal places by default (Yen for example does not have decimal places at all)
Now, i use Decimal.js to execute calculations with these values
For example i am multiplying with a percentage
let d = new decimal("29.00")
let e = d.mul(0.133333333333).toDP(d.decimalPlaces())
The result of this however is rounded to 0 decimal places as the constructer strips away the trailing zeros and sets decimalPlaces to 0.
How can i get a decimal value that always has to amount of decimal places provided by the input string? In this example d.decimalPlaces should return 2 (because 29.00 has to decimal places).
Alternative solution: How do i extract the number of decimal places out of the string?
You mean this?
const keepDecimal = (str,mul) => {
const dec = str.split(".");
const numDec = dec.length===2?dec[1].length:0;
return (str*mul).toFixed(numDec);
}
console.log(keepDecimal("29.13",0.133333333333))
console.log(keepDecimal("29",0.133333333333))

Round Down Decimal Places Javascript

I need a rounding down many many decimal places down, basically roundTo but supposedly rounding down at the spot. Example,
take the number, 1.087179939485353505
but to the fifth place, with roundTo of 6
roundTo(1.087179939485353505, 6) is 1.08718
I need 1.08717 not 1.08718 in javascript.
var Variable = roundTo(Variable / 1000000000000000000, 6);
Resolved
There seems to be no native javascript decimal rounding function that rounds down. One of two options are available.
Convert to string and manipulate the data that way (makes the most sense).
Utilize a number and multiply, floor then re-divide again for your number
How about convert to string, slice and convert back to number.
const roundTo = num => Number(String(num).slice(0, 7));
console.log(roundTo(1.087179939485353505));
You could use regex to get 5 decimals
function roundTo(number) {
var result= number.toString().match(/^\d+(\.\d{0,5})/)[0];
console.log(result);
}
roundTo(1.087179939485353505);
var Variable = Variable / 1000000000000000000;
Variable *= 1000000;
Variable = Math.floor(Variable);
Variable /= 1000000;
var Variable = roundTo(Variable, 6);
I took my decimal, multiplied it by how many places I wanted to roundTo, math floor for absolute low rounding and divided it once again before lastly using roundTo for the precise decimal place. Seems the only way.

Converting floating point numbers to integers, rounding to 2 decimals in JavaScript

What's the best way to perform the following conversions in JavaScript? I have currencies stored as floats that I want rounded and converted to integers.
1501.0099999999999909 -> 150101
12.00000000000001 -> 1200
One way to do this is to use the toFixed method off a Number combined with parseFloat.
Eg,
var number = 1501.0099999999999909;
var truncated = parseFloat(number.toFixed(5));
console.log(truncated);
toFixed takes in the number of decimal points it should be truncated to.
To get the output you need, you would only need `toFixed(2)' and multiple the result by 100.
Eg,
var number = 1501.0099999999999909;
var truncated = parseFloat(number.toFixed(2)) * 100;
console.log(truncated);

whole number in javascript?

I get 28.6813276578 when i multiply 2 numbers a and b, how can i make it whole number with less digits
and also, when i multiply again i get results after first reult like 28.681321405.4428.68 how to get only one result ?
<script>
$(document).ready(function(){
$("#total").hide();
$("#form1").submit(function(){
var a = parseFloat($("#user_price").val());
var b = parseFloat($("#selling").val());
var total = a*b;
$("#total").append(total)
.show('slow')
.css({"background":"yellow","font-size":50})
;
return false;
});
});
</script>
You can do several things:
total = total.toFixed([number of decimals]);
total = Math.round(total);
total = parseInt(total);
toFixed() will round your number to the number of decimals indicated.
Math.round() will round numbers to the nearest integer.
parseInt() will take a string and attempt to parse an integer from it without rounding. parseInt() is a little trickier though, in that it will parse the first characters in a string that are numbers until they are not, meaning parseInt('123g32ksj') will return 123, whereas parseInt('sdjgg123') will return NaN.
For the sake of completeness, parseInt() accepts a second parameter which can be used to express the base you're trying to extract with, meaning that, for instance,
parseInt('A', 16) === 10 if you were trying to parse a hexidecimal.
See Math.round(...).
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Math/round
In addition to the other answers about rounding, you are appending the answer to "total" by using
$("#total").append(total)
You need to replace the previous text rather than appending by using
$("#total").html(total)

Categories