This question already has answers here:
How to format numbers as currency strings
(67 answers)
Closed 9 years ago.
I am new to JavaScript so would appreciate help on this issue I have.
I have a booking form I have made and I have 3 Eur results that come up automatically following the selection choices made (#Martina helped me with this challenge).
What I would like to do is get the results to show with 2 decimal places only - here is the link to the page I am talking about:
https://www.alpinemalta.net/oilandgaslibya/bookNow.html
I've tried looking through some of the posts in this forum and attempted some of the things but my newness to JS has not helped :(
Thanks again in advance for any help on this.
Cheers - Chris Brown
The Number class in Javascript has a .toFixed method that does exactly what you require, e.g.:
var n = 234.1
var s = n.toFixed(2); // s = "234.10"
Related
This question already has answers here:
"Variable" variables in JavaScript
(8 answers)
Closed last month.
So, I am working on a wheel of fortune.
I have a const list with a lot of questions.
Underneath the const list is a randomizer that picks a random question.
The thing is that on the wheel of fortune segments it displays the random question but I want to show the const name.
After it stops spinning it indicates the segment and make an alert with the question.
const questioncard = ['question1','question2','question3']
const randomquestioncard = questioncard[Math.floor(questioncard.length * Math.random())];
Why don't you keep the data like this:
[{question:'question1', text:'text of the question 1'},{question:'question2', text:'text of the question 2'},{question:'question3', text:'text of the question 2'}]
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have a situation where I get 4, 5 or 6 images/tiles.
Depending on the number of tiles, I need to format the images on the webpage.
Like this http://prntscr.com/9y75dw
If it's five images, I have to format it in such a way that two images in the first row and three images in the second row. Can you help me with the logic?
Well I don't see a technique, maybe I am missing to do that more appropriately or in a generic way but since the description in less and number of images given are random, I don't know how this will work.
var imageLength = $('img').length;
var newLength = 0, differenceLength=0;
if(imageLength%2==0){
//incase of even number
//Do what you like here eg: $('img').css('width', '50%');
}
else{
// incase of odd number
newLength = Math.round(imageLength/2); //dividing number into two parts.
differenceLength = imageLength - newLength; //difference to put smaller above and greater below.
$('parent-div img:nth-child(1)').nextUntil('img:nth-child('+differenceLength+')').wrapAll('<div></div>') //wraps into a container div
}
Although this is just one way. You might have already realized a lot of logic by now.
PS: I have randomly written this code so take it as a logic for help. Not sure whether this will work.
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 7 years ago.
Why the result of this data is doesn't give the exact value expected by a human brain or expected output of a person to appear
16.08 * 100 = 1607.9999999999998;
When i tried it to the console developer of the chrome browser;
and i tried using
console.log(16.08*100); gives 1607.9999999999998
Supposedly the answer that i should be seeing is
16.08 * 100 = 1608;
will you please help me with these. or any explanations
use .toFixed(n) method of javascript.
console.log((16.08 * 100).toFixed());
This question already has answers here:
Javascript Countdown
(6 answers)
Closed 9 years ago.
I am trying get a counter to work in PHP so it basically counts '5' then '4' and so on till 0. It will redirect to a different page. I was just wondering how this can be done in PHP? if possible, if not what it would be in javascript?
Take a look at this. Other than that … you should really try to do some original research yourself and ask meaningful questions if you are stuck by illustrating your problem and showing us your current solution/approach.
The following code will redirect after 5 seconds:
<script>
setTimeout(function () {
window.location.href = "newpage.html";
}, 5000); //seconds
</script>
here's a example of count down timer: link
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Is JavaScript's Math broken?
I'm calculating the sum of several float values using javascript and... I've noticed a strange thing never seen before. Executing this code:
parseFloat('2.3') + parseFloat('2.4')
I obtain 4.699999999999999
So... what sould I do to obtain a correct value? (supposed that this is incorrect...)
Once you read what What Every Computer Scientist Should Know About Floating-Point Arithmetic you could use the .toFixed() function:
var result = parseFloat('2.3') + parseFloat('2.4');
alert(result.toFixed(2));
(parseFloat('2.3') + parseFloat('2.4')).toFixed(1);
its going to give you solution i suppose