d3.format wont keep trailing zero - javascript

I'm using d3.round(num,2) to round a number from say, 2.567 to 2.57. My problem is that when I do this, I want 2 decimal numbers of precision to be used at all times/regardless of if it is a zero.
when I have a number like 2.201, I want the display to be 2.20, and instead am coming up with 2.2. Is there any way to format the round function to always include zeroes?
Thanks for any help!
Edit: used num.toFixed(2) and that works, but I am returning d3.format("$")(num.toFixed(2)) and that is only returning $2.3, which is the reason I needed this, I am looking to display a price. If anyone has help to offer with that, it would be great, thank you

Try this:
d3.format("$.3n")(num);

Related

Formatting a changing number

I would like to format an increasing number to make it have 2 digits after the decimal place at all times, but I can't seem to make it happen. I have tried using if statements to add 2 zeroes to the end of it, but this method doesn't work very well. Please may I have some help? Thank you!
My code:
if (round2(num)<floor(num)+0.001) {
document.getElementById('number').innerHTML='number
'+round2(num)+'.00';}
Another method that I have tried:
if (round2(num)<floor(num*10)/10+0.001) {
document.getElementById('number').innerHTML='number
'+round2(num)+'.0';}
var xNumber = 23.58841;
console.log(xNumber.toFixed(2));

Adding a whole number and a decimal in Javascript results in removal of the decimal?

I'm sure this is simple, but in my javascript code, I have two numbers. One contains a decimal, and the other doesn't, and I add them together (ie. 7.5 + 5), I am getting a result with NO decimal value.
Do I need to cast each number variable as a double? I know that all numbers are doubles in javascript - which is why I do not understand this behavior...
For instance, I have var answer = week1 + week2;. Does this make sense?
Thanks in advance!
I am sorry for wasting time - turns out I was using parseInt instead of parseFloat to gather the "week" values I spoke about.
Can someone please close this question or delete it? Before the shame consumes me?

toFixed hanging my code

I am taking practice exercises and ran into this issue.
This problem asks:
“Prompt for a number greater than 1 and to 4 decimal places. Format
and write the number to the page displaying with only 2 digits past
the decimal point using the toFixed() method. (e.g. 12.35, not
12.3453)
Since this method is very new, it doesn't work in older browsers. See
if you can get only 2 digits past the decimal point to show without
using toFixed().”
I found the answer to the hard part through the archives here, Math.round(n*100)/100. Thanks for that. But when I tried the “easy” way, I get nothing. My work is at jsFiddle, but in a nutshell:
var num = prompt("Give me a number greater than one, with 4 decimal places.");
var num2 = prompt("Great! Do one more, please!");
num = Math.round(num*100)/100;
num2 = num2.toFixed(2);
alert(num);
alert(num2);
The exercise did not ask for a second number, but I wanted to use both methods in separate incidences. When I run this it does not alert anything. I know that it is hanging at the toFixed statement, because when I comment it out it alerts both as expected, num1 at 2 decimal places, and num2 as it was prompted (i.e. 1.2345).
So here is what I have done so far:
Mozilla’s developer page shows this format: n.toFixed(1);// Returns "12345.7": note rounding
Seems exactly what I am doing.
I copied and pasted all of it in my Sublime, making sure that I called the .js file just before the closing body tag, to make sure it wasn’t some loading problem I don’t understand.
Plus all sorts of little tweaking.
Sorry to keep asking these questions, but since I am self-study, I have where else to go!
You need to use parseFloat. Using the function will convert the string to a float type variable, allowing for the toFixed function to work properly.
num2 = parseFloat(num2).toFixed(2);

Having trouble building a calculator

I am trying to build a simple calculator to learn javascript/jquery but seem to be getting quite confused on how to make it work. Basically at the moment I dont seem to be able to update my tally correctly, instead of the numbers/values being added together in a sum they are being concatenated instead, if anyone could give me some guidance on how I can get back on the right path with this and any other advice on how to make the code more efficient then that would be great. I don't want anyone to give me the correct script just yet.
http://jsfiddle.net/kyllle/edRk9/4/
Thanks all in advance for your advice
The val() method returns a string, and the + operator will concatenate if one of the operands is a string. You can convert the value to a number using parseInt[MDN]:
$('input.num', '#buttons').on('click', function() {
// when click a number it appears in #result
currentNumber = parseInt($(this).val(), 10);
$('#result').html(currentNumber);
});
Note, for parsing decimal numbers, use parseFloat[MDN], not parseInt.
parseInt, parseFloat or multiply value by 1 before sum or use eval(), but it's the worst case :)

Is there a way to truncate scientific notation numbers in Javascript?

As you all know since it is one of the most asked topic on SO, I am having problems with rounding errors (it isn't actually errors, I am well aware).
Instead of explaining my point, I'll give an example of what possible numbers I have and which input I want to be able to obtain:
Let's say
var a = 15 * 1e-9;
alert(a)
outputs
1.5000000000000002e-8
I want to be able to obtain 1.5e-8 instead, but I cannot just multiply by 10e8, round and divide by 10e8 because I don't know if it will be e-8 or e-45 or anything else.
So basically I want to be able to obtain the 1.5000002 part, apply toFixed(3) and put back the exponent part.
I could convert into a string and parse but it just doesn't seem right...
Any idea ?
(I apologize in advance if you feel this is one of many duplicates, but I could not find a similar question, only related ones)
Gael
You can use the toPrecision method:
var a = 15 * 1e-9;
a.toPrecision(2); // "1.5e-8"
If you're doing scientific work and need to round with significant figures in mind: Rounding to an arbitrary number of significant digits
var a = 15 * 1e-9;
console.log(Number.parseFloat(a).toExponential(2));
//the above formula will display the result in the console as: "1.50e-8"

Categories

Resources