How can i make the calculation not display decimals? [duplicate] - javascript

This question already has answers here:
How can I remove the decimal part from JavaScript number?
(16 answers)
Closed 2 months ago.
How can i make the result of the calculation to display no decimals - for all the results ?
now the number is variable on a slider that gives a number from : 5000 - 100000
and is then divided or multiplied with : X
the problem if X is somthing like : * 0.0001097 the number i is a mile long
i just want the result to be wihtout decimals
i am a real amateur in this field.. have mercy with me hahah
function do_on_range_change_pages() {
$('.betrag').text(pages);
$('.name1').text((pages * 0.001));
$('.name2').text((pages * 0.03));
$('.name3').text((pages / 22));
}
*how can i make the result of this display no decimals ?*
$('.name3').text((pages / 22 **?????**));

In Javascript, you could remove decimals with severals solutions !
The parseInt() function is the slower (!).
let myNumber = 5.0214;
let myOtherNumber = 5.9;
// With Math.floor() method
let myNymberWithoutDecimals = Math.floor(myNumber) // 5
let myOtherWithoutDecimals = Math.floor(myOtherNumber) // 5
// With Math.round() method
let myNymberWithoutDecimals = Math.round(myNumber) // 5
let myOtherWithoutDecimals = Math.round(myOtherNumber) // 6
// with parseInt()
let myNymberWithoutDecimals = parseInt(myNumber) // 5
let myOtherWithoutDecimals = parseInt(myOtherNumber) // 5
// with toFixed(number)
myNumber.toFixed(2) // 5.02
myNumber.toFixed(1) // 5.0
myNumber.toFixed() // 5
With your example :
function do_on_range_change_pages() {
$('.betrag').text(pages);
$('.name1').text((pages * 0.001).toFixed());
$('.name2').text((pages * 0.03).toFixed());
$('.name3').text((pages / 22).toFixed());
}
Better advice : new to JS ? Try to use google.
My research : https://www.jsdiaries.com/how-to-remove-decimal-places-in-javascript/
Try https://beta.sayhello.so/, this search engine could find snippets :) !
Have a nice day :)

Related

How to generate a random number from 2 numbers [duplicate]

This question already has answers here:
How to decide between two numbers randomly using javascript?
(4 answers)
Closed 1 year ago.
I've searched a lot for generating a random number but all I got is generating for a range between a or b.
I'm trying to get a number from a or b, i.e. either a or b, none from in between.
This returns the first value only
var number = 1 || 9; \\9
You can store your two numbers in an array and get a random index of that array. Here's an example:
var yourTwoNumbers = [2,5]
console.log(yourTwoNumbers[Math.floor(Math.random() * yourTwoNumbers.length)]);
So Math.random() randomly generates a number between 0.0 and 1.0. Math.random() < 0.5 has a 50% percent chance of either being true or false. This way you can select one of two numbers with equal probability.
let number = Math.random() < 0.5 ? 1 : 9;
console.log(number)
The same asked here: How to decide between two numbers randomly using javascript?
There is already an answer with explanations.
The Math.random[MDN] function chooses a random value in the interval [0, 1). You can take advantage of this to choose a value randomly.
const value1 = 1
const value2 = 9;
const chosenValue = Math.random() < 0.5 ? value1 : value2;
console.log(chosenValue)

Javascript to turn 110,000 into 110K and not 0.11M [duplicate]

This question already has an answer here:
Format a javascript number with a Metric Prefix like 1.5K, 1M, 1G, etc [duplicate]
(1 answer)
Closed 7 years ago.
To start with you need...
function m(n,d){x=(''+n).length,p=Math.pow,d=p(10,d)
x-=x%3
return Math.round(n*d/p(10,x))/d+" kMGTPE"[x/3]}
Then calling like so...
// m( ANY NUMBER HERE or VAR LIKE I USE,HOW DECIMAL PLACES)
m(110000,2)
However instead of the above's result of 0.11M, I would like it to display 110k.
What you have there is an example of an overly optimized script, lets make it more developer friendly an readable
function metricPrefix(rawNumber,decimalPlaces){
var sufixes= " kMFGPE";
var numberLength =(''+n).length;
decimalPlaces=Math.pow(10,d); //raise 10 to the number of decimal places
var modLen = numberLength - numberLength%3;
var sufix = sufixes[modLen/3];
return Math.round(rawNumber*decimalPlaces/decimalPlaces(10,modLen))/decimalPlaces+ sufix;
}
Now it's easier to work with. We can see the issue is that we need to adjust for when the string is divisible by 3, so lets fix that.
function metricPrefix(rawNumber,decimalPlaces){
var sufixes= " kMFGPE";
var numberLength =(''+rawNumber).length;
decimalPlaces=Math.pow(10,decimalPlaces); //raise 10 to the number of decimal places
//THis is the change
//If the length is divisable by 3 take 3 off the length
var modLen = numberLength%3 == 0 ? numberLength - 3 - (numberLength%3) : numberLength - (numberLength%3);
console.log(modLen);
var sufix = sufixes[(modLen/3)]
console.log(sufix)
return Math.round(rawNumber*decimalPlaces/Math.pow(10,modLen))/decimalPlaces+ sufix;
}
$(document).ready(function(){
$("#result").html(metricPrefix(110000,2));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="result"></div>

JavaScript check if number is whole [duplicate]

This question already has answers here:
How do I check that a number is float or integer?
(52 answers)
Closed 7 years ago.
im trying to check if a number is a whole after a calculation. What I have so far prints out how many times one number gets divided by another, but when the number is not whole it dose not print anything out. Heres my code:
function round() {
var percent = document.getElementById('percent_sale').value;
var perShare = document.getElementById('singleShare').value;
var result = (percent / perShare);
if(result % 1 == 0) {
document.getElementById('results1').innerHTML = ('Number of shares:'+result);
} else {
document.getElementById(results1).innerHTML = ('number of shares must ');
}
}
The values get input buy a user, and the percent for sale is say 50 and the single share is say 2.5 this would return 20 shares.
What I need is if I put in something like 50 for sale and 3.15 single share it tells the user to make equal number of shares as it would return 15.87
Any ideas where ive gone wrong?
Convert your number into string and then check if the string contains only numbers
var num = 15;
var n = num.toString();
This will convert it into string then this
String.prototype.isNumber = function(){return /^\d+$/.test(this);}
console.log("123123".isNumber()); // outputs true
console.log("+12".isNumber()); // outputs false
For further reference.Link StackOverFlow

how to get 1.450 = 1.5 in javascript? (round to 1 decimal place) [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do you round to 1 decimal place in Javascript?
My Value is 1.450 and I have to round it to 1 decimal place.
I want 1.450 = 1.5 in Javascript can any body fix this please.
You need this:
var mynum = 1.450,
rounded = Math.round(mynum * 10) / 10;
suppose you have
var original=28.453;
Then
var result=Math.round(original*10)/10 //returns 28.5
From http://www.javascriptkit.com/javatutors/round.shtml
You can also see How do you round to 1 decimal place in Javascript?
Given your fiddle, the simplest change would be:
result = sub.toFixed(1) + "M";
to:
result = Math.ceil(sub.toFixed(1)) + "M";
If you use Math.round then you will get 1 for 1.01, and not 1.0.
If you use toFixed you run into rounding issues.
If you want the best of both worlds combine the two:
(Math.round(1.01 * 10) / 10).toFixed(1)
You might want to create a function for this:
function roundedToFixed(_float, _digits){
var rounder = Math.pow(10, _digits);
return (Math.round(_float * rounder) / rounder).toFixed(_digits);
}

Javascript number placing and how to , 4 = 1.04, 14 = 1.14, 100 = 2.00 [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Javascript number placing and how to , 4 = 0.04, 14 = 0.14, 100 = 1.00
I am trying to write a custom calculator but I am having trouble trying to work out a figure, I want to be able to add decimal points before the number which has been inputed.
For example if the user puts in 4 I want the value in the string to look like this 1.04 and so on 14 = 1.14, 100 = 2.00.
I tried using the inbuilt function
var num = 4; fig = num.toFixed(2);
But that doesn't work, the only way I can think to do it is with if(val.length >2){ do something; } which would be a long way to do this. Has any body got any ideas for this?
This is very similar to your previous question: Javascript number placing and how to , 4 = 0.04, 14 = 0.14, 100 = 1.00
Let's say your user enters a figure which you assign to variable num. The result is given by
var result = 1 + (num / 100)
You could also just add 1 to the result given by any of the answers to your previous question.

Categories