Currently when using numeral(1000000).format('0a') will return 1m, however is it possible to set a scale, for example thousands, so any number will get converted to a number in thousands?
For instance 1000000 will become 1000k, and 100 would become 0,1k and so on.
console.log(
numeral(1000000).format('0a')
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/numeral.js/2.0.6/numeral.min.js"></script>
Try 0ak format
https://github.com/adamwdraper/Numeral-js/blob/master/src/numeral.js#L143
console.log("1000000:",numeral(1000000).format('0ak'),"\n100:",numeral(100).format('0ak'))
<script src="//cdnjs.cloudflare.com/ajax/libs/numeral.js/2.0.6/numeral.min.js"></script>
Related
I have a number that increases every second by addition. The problem is that it contains way too many decimals. An example of an output which I do not want would be something like 1.5999999999999999 or 1.600000000001. I have tried using the toFixed method, passing in 2 as it's value, and I am still getting the issue. I have also tried using the Math.round(number * 100) / 100 and am still getting uggly decimals.
How can I get an output that only contains a specified number of decimal points? I want to be able to choose how the number rounds. The function to round should be something like this:
function round(numberToRound, afterDecimals){
//Rounding code which takes into the account the number of decimal values I wish to display should go here
}
Thank you.
I am so sorry, I didn't call the function in the right place. The function works as expected, I just wasn't updating the innerHTML correctly.
Again, I am sorry for overlooking this.
How would I make it so that when hovering over a plotly graph, the displayed value in the hoverbox can be divided by 1000 and have a "K" appended to it?
For instance, when hovering over "$10,000,000" in a plotly graph, what could be done so that "$10,000K" is displayed in the box?
Specifically, can anything be done with the "hoverformat" property? It currently looks like this:
hoverformat: ",.0f"
I'm aware that this prints a float with 0 decimal places...but how would I specify the displayed value to be divided by 1000 and then have a "K" appended?
Thanks.
You might consider using numeral for that purpose. Numeral is a library used to format numbers and you can find it here:
http://numeraljs.com/
You then could do something like
const number = 10000;
const formattedNumber = numeral(10000).format('$ 0.00 a');
I have a some JavaScript that I use to calculate a percentage discount on a form field that holds a total value.
var thirteen = 10;
var percent_discount = (thirteen) * 0.01;
var percent_discount_final = (382.50 * (percent_discount));
console.log(percent_discount_final.toFixed(2));
The calculation is outputted at 38.24??
Why is that happening?
Well, should be the full value minus the discount - third line being:
var percent_discount_final = value_of_some_form_field - (value_of_some_form_field * percent_discount);
You almost figured out the right multiplier, but you forgot to subtract from 1, so you are multiplying by 0.1 instead of 0.9.
Here is an example of how to subtract any percentage from a number.
const subtractPercent = (value, percentage) => {
const multiplier = 1 - (percentage / 100);
return value * multiplier;
};
subtractPercent(250, 10); // 225
subtractPercent(250, 3); // 242.5
Similar to yours, this function assumes that value is positive, for simplicity's sake. Negative values will add instead of subtract.
subtractPercent(250, -10); // 275
subtractPercent(-250, 10); // -225
fun fact - float type is not precise. it came from large numbers theory, and by design it is approximation. that is why several languages provide also types like monetary etc. and for languages that don't, we calculate all monetary stuff on integers. it's not javascript related "bug", it is computer science "feature" :)
how to calculate monetary stuff on integers? well, you have to create/emulate new type. general idea is
convert all data from float to integer multiplying also by 100
perform all calculations on integers (no single float can be mixed, or the result will also be converted to float)
at the end (in most cases it's on data output / user interface) divide every value by 100
yes, in 21 century it looks plain stupid, but there is no magic you can use, when your tool (JS) doesn't have dedicated data type for monetary calculations. you have to build it.
is JS the only language involved in this project? if there is some for example some SQL db, most of them have dedicated monetary types, so you can make all your calculations on database side.
I have a JavaScript calculator which uses the Math.cbrt() function. When I calculate the cube root of 125 it returns 4.999999999999999. I understand that I could use Math.round() to round any answers that this function returns to integer values, but I do not want to do that exactly. Is there a way to use this if and only if the result of calculation is some number followed by a string of 9s (or something similar like 4.99999998 perhaps) after the decimal?
What you are dealing with is the frustration of floating point numbers in computing. See the Floating Point Guide for more information on this critical topic.
The short version:
Certain non-integer values cannot be represented accurately by computers, so they store a value that is "near enough". Just try evaluating 3.3 / 3 in your favourite REPL.
Say what?!
Computers are supposed to be perfect at this numbers/math thing, right? Can I trust any answer they give me?
Yes, for integers, they are pretty much perfect. But for non-integer calculations, you should assume that answers won't be exact, and account for these floating point errors.
The solution in Javascript
In newer versions of Javascript, you have a defined constant Number.EPSILON, which is the smallest difference between the actual number and the approximation that it can actually store.
Using this, you can multiply that constant by the result you get and add it to the result and you should get the exact value you require.
function cbrt(n) {
return Math.cbrt(n) + (Number.EPSILON * Math.cbrt(n));
}
Alternatively, you can use the rounding behaviour of the .toFixed() method on numbers together with the parseFloat() function if you only care about numbers up to a certain number of decimal places (less than 20).
function num(n, prec) {
if (prec === void 0) prec = 8; // default to 8 decimal places
return parseFloat(n.toFixed(prec));
}
var threshold = 0.999; // set to whatever you want
var fraction = result % 1;
if (fraction >= threshold) {
result = Math.round(result);
}
I understand that JS math is not perfect. but how can i format this to get the correct answer as I have a cart item which costs .60 cents and they can change the quantity?
var a=3*.6;
document.write(a);
writes 1.7999999999999998
Obviously I want to write 1.8. any ideas how to accomplish this?
Use toFixed to round it back:
var a = 3*.6;
document.write(a.toFixed(2));
If you need it as a number, add a + sign before it:
var a = 3*.6;
console.log(+a.toFixed(2)); // Logs: 1.8, instead of "1.80"
var a=3*.6;
a = Math.round(a*10)/10;
document.write(a);
Since you want to round to the 10ths place, you need to multiply the number by 10, round the result of that multiplication to the nearest whole number, and then divide the result by 10.
It's not sexy, but ya gotta do whatchya gotta do.
var a=(3*(.6*100))/100;
document.write(a);
Example: http://jsfiddle.net/aJTJq/
multiply .6 by 100 to get the 60 cents
multiply that by 3
divide it by 100 to return it as a dollar figure
Write the .6 as a fraction: a=3*6/10 and you get 1.8
As a more general rule, you could try rounding to the nearest millionth with
Math.round(result*1000000)/100000 and seeing what that gets you.
What you want to do is have some type of rounding function.
Try this:
Rounding Function
<script language="javascript" type="text/javascript">
function roundNumber(rnum, rlength) { // Arguments: number to round, number of decimal places
var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
document.roundform.numberfield.value = parseFloat(newnumber); // Output the result to the form field (change for your purposes)
}
</script>
And then something like this to call the function
<form name="roundform">
<input type="text" name="numberfield" value="">
<input type="button" value="Round" onClick="roundNumber(numberfield.value, 2);">
</form>
This example just takes a number in the text field and ensures that it is rounded to two decimal places.
This was taken from http://www.mediacollege.com/internet/javascript/number/round.html
There are more examples on this link as well. Hopefully this helps.
Cheers