I'm trying to create a function with javascript that displays all numbers with two decimal points and adds commas every three digits (1,000 10,000 100,000 etc).    At first I had this:    var formatNumber = function(num) {     return parseFloat((num).toFixed(2)).toLocaleString(); };   This works very well but with one exception.  1.2 != 1.20 in the final output.  It just shows up as 1.2  But everything else is good.  124.5879697 = 124.59, 10000 = 10,000, and 10586.357 = 10,586.36  The issue is the final output is going to display as money, so displaying 10000 as $10,000 is fine.  But displaying 1.2 as $1.2 looks a little off.  To get around this I tried to add the following modification:   var formatNumber = function(num) {    return parseFloat((Math.round(num*100)/100)).toFixed(2).toLocaleString(); };   This carries everything out to two decimal places, which is fine, but it seems to have de-activated toLocaleString because now nothing displays commas.    I'm looking to use pure Javascript with this and not jQuery.  It seems there are a lot of js questions on this topic about one or the other issue, but not combining both.  Thank you.
715
I would like to format a price in JavaScript. I'd like a function which takes a float as an argument and returns a string formatted like this: &quot;$ 2,500.00&quot;  What's the best way to do this?
7604
I keep stumbling on the format specifiers for the printf() family of functions.  What I want is to be able to print a double (or float) with a maximum given number of digits after the decimal point.  If I use:  printf("%1.3f", 359.01335); printf("%1.3f", 359.00999);   I get  359.013 359.010   Instead of the desired  359.013 359.01   Can anybody help me?
8965
According to the documentation, the  method uses a round-to-even algorithm which is not common for most applications. So I always end up writing a custom function to do the more natural round-half-up algorithm:  public static decimal RoundHalfUp(this decimal d, int decimals) {     if (decimals &lt; 0)     {         throw new ArgumentException("The decimals must be non-negative",              "decimals");     }      decimal multiplier = (decimal)Math.Pow(10, decimals);     decimal number = d * multiplier;      if (decimal.Truncate(number) &lt; number)     {         number += 0.5m;     }     return decimal.Round(number) / multiplier; }   Does anybody know the reason behind this framework design decision?  Is there any built-in implementation of the round-half-up algorithm into the framework? Or maybe some unmanaged Windows API?  It could be misleading for beginners that simply write decimal.Round(2.5m, 0) expecting 3 as a result but getting 2 instead.
60259
What is the difference between decimal, float and double in .NET?  When would someone use one of these?
85536
I thought I was able to do something like this before, but I can't find my solution.  I would like to format my large numbers as follows:   12,504.3124 2359 3   I know I can use siunitx to partially set this up, but I'm not getting the results I want:  \documentclass{article} \usepackage{siunitx}  \newcommand\aenumA[1][4]{%%   \num[group-separator={,},        group-digits=integer,        ]}  \newcommand\aenumB[1][4]{%%   \num[group-digits=decimal,        ]}  \pagestyle{empty} \begin{document}    \aenumA{12504.312423593}    \aenumB{12504.312423593}  \end{document}   I would like a hybrid solution (commas in the integer part and spaces in the decimal).  I would actually also like to be able to group differently between the two: every three digits in the integer part, every four digits in the decimal part.  Is there a straight-forward way to do this with siunitx?  Or, do I need to split the number at the decimal and treat the two parts separately and then glue them back together?
62686
I have numbers in cells in Excel. I want the numbers formatted so that if they have decimal places they show to a maximum of two, and if they have no decimal places it doesn't show any.  For example.    15 should be formatted as 15, NOT 15.00 14.3453453 should be formatted as 14.35 12.1 should be formatted as 12.1  0 should be formatted as 0   The closest custom format code I've come up with is 0.##. Unfortunately this formats 15.00 as 15. (note the extra decimal point).  To further complicate the issue, the spreadsheet is a result of an export from SQL Server Reporting Services. So no macros are possible. Oh well, it looks like 0.## is my best bet, and they can just live with the extra period.
2827
What's the cleanest, most effective way to validate decimal numbers in JavaScript?  Bonus points for:   Clarity. Solution should be clean and simple. Cross-platform.   Test cases:  01. IsNumeric('-1')      =&gt; true 02. IsNumeric('-1.5')    =&gt; true 03. IsNumeric('0')       =&gt; true 04. IsNumeric('0.42')    =&gt; true 05. IsNumeric('.42')     =&gt; true 06. IsNumeric('99,999')  =&gt; false 07. IsNumeric('0x89f')   =&gt; false 08. IsNumeric('#abcdef') =&gt; false 09. IsNumeric('1.2.3')   =&gt; false 10. IsNumeric('')        =&gt; false 11. IsNumeric('blah')    =&gt; false
13722
I'm looking for a good JavaScript equivalent of the C/PHP printf() or for C#/Java programmers, String.Format() (IFormatProvider for .NET).  My basic requirement is a thousand separator format for numbers for now, but something that handles lots of combinations (including dates) would be good.  I realize Microsoft's  library provides a version of String.Format(), but we don't want the entire overhead of that framework.
47935
I need to convert lat/long that is expressed as degrees, minutes, and seconds in the data into decimal degrees. For example, in the data, they are listed as N335042.06 in the Latitude column, and W86031.04 in the Longitude column. I have done this problem before where I created a script which converted DMS to DD, and vice-versa, so I guess I could use bits from that. But the problem I'm having is how to ignore (for lack of a better word) the 'N' and 'W' in the data? Can I skip them? And DMS are listed all together without any symbols or spaces.  Can I use len(), range(), split() to specify what part to read from the value? For example, can do the following?  N335042.06 where, 33 = degrees 50 = minutes 42.06 = seconds ...?  I came across  ESRI article, but it's in VB. Will probably use it as a reference but some of the terminology/syntax is different from Python.  Final code that works!   # Pre-logic def latDD(x):   D = int(x[1:3])   M = int(x[3:5])   S = float(x[5:])   DD = D + float(M)/60 + float(S)/3600   return DD  # Expression latDD(!Latitude!)
40138
What's the proper way to convert from a scientific notation string such as "1.234567E-06" to a floating point variable using C#?
31758
The documentation for the  function states that you pass it a number, and the positions past the decimal to round. Thus it should do this:  n = 5.59 round(n, 1) # 5.6   But, in actuality, good old floating point weirdness creeps in and you get:  5.5999999999999996   For the purposes of UI, I need to display 5.6. I poked around the Internet and found some  that this is dependent on my implementation of Python. Unfortunately, this occurs on both my Windows dev machine and each Linux server I've tried. .  Short of creating my own round library, is there any way around this?
38898
I need to format a decimal value into a string where I always display at lease 2 decimals and at most 4.   So for example  "34.49596" would be "34.4959"  "49.3" would be "49.30"   Can this be done using the String.format command? Or is there an easier/better way to do this in Java.
39431
I am trying to plot data from data files which are using the comma as decimal separator instead of a point (as is normal in SI style (French version):  ). However, even after excessive search I cannot find an option that would actually tell pgfplots to read the table with the comma as decimal separator. Minimal (not) working sample:  \documentclass[paper=a4,12pt,version=last,landscape]{scrartcl} \usepackage{tikz} \usepackage{pgfplots} \begin{document} \centering \begin{tikzpicture}     \begin{axis}[             width=0.9\textwidth,             height=0.5\textheight,             xlabel={Standardweg~[mm]},             ylabel={Standardkraft~[N]},             grid=major,             ]         \addplot table[x=Standardweg, y=Standardkraft] {Messwerte.TRA};     \end{axis} \end{tikzpicture} \end{document}   Messwerte.TRA:  Standardweg  Standardkraft 0,000000000000e+000  1,960904836655e+000 -2,349615044750e-004     3,081407308578e+000 -2,349615044750e-004     4,164415359497e+000 -2,349615044750e-004     5,441759109497e+000 -2,349615044750e-004     6,443712234497e+000 -2,349615044750e-004     7,598009109497e+000 -2,349615044750e-004     8,951524734497e+000 -6,029571522959e-001     1,002574348450e+001 -2,349615044750e-004     1,122496223450e+001 -2,349615044750e-004     1,252183723450e+001   On compilation, I get the error: ! Package PGF Math Error: Could not parse input '-2,349615044750e-004' as a floating point number, sorry. The unreadable part was near ',349615044750e-004'.. for every single number in the .TRA-file.  I know that I could find and replace every , with a . in the files, but that is not an option considering the amount of files I have to evaluate.  Also, I tried stuff like /pgf/number format/use comma, set decimal separator={{,}}, \pgfkeys{/pgf/number format/.cd,fixed,precision=2,use period} and use comma, but none of that helped and I am pretty much running out of options. The documentation for pgfplots did not give any other ideas, either.  So: How do I convince pgfplots to actually read the comma as decimal separator and to not search for the point?
29340
I'm using Babel for writing a document in spanish.  However, it uses a comma instead of a period as a decimal marker (i.e. 3,14 instead of 3.14).  Is there any way to reverse the behaviour? I prefer the period, as it's done in english.   Also, I'd prefer it if it's something that can just be added to the preamble, instead of having to do it each time I want to write a number with a decimal part (like putting the period between brackets, for instance).
31864
I'm using ArcMap 10.3. In symbology, there are 6 decimal digits for the different classes of the variable I'm presenting. I'd like to show only 3 decimal digits. I can edit the label and change the decimals manually for each class. Is there any faster way to do this for all classes in ArcMap 10.3?
29904
If I have a double (234.004223), etc., I would like to round this to x significant digits in C#.  So far I can only find ways to round to x decimal places, but this simply removes the precision if there are any 0s in the number.  For example, 0.086 to one decimal place becomes 0.1, but I would like it to stay at 0.08.
80556
What's going on?! Subtraction works fine until I get to 0.1 - 0.1. I'm in visual c# 2008 using the nonoba.com API.  Console.WriteLine("hit! " + Users[targetNum].character.health + " : " + player.character.profile.attackPower);  Users[targetNum].character.health -= player.character.profile.attackPower;  Console.WriteLine("health! " + Users[targetNum].character.health);   output:  hit! 0.1 : 0.1 health! 1.490116E-08   Thanks all - I might use the decimal type, as I'm normally adding/subtracting nice "round" numbers. For now I'll just go with:  if (Users[targetNum].character.health &lt;= 0.00001)   By the way I knew this wasn't really going to be a "bug" in c# - I thought it would either by a bug in my code or some lack of understanding, which it was.  Having read all the recommended reading, I'm going to conclude that my folly was due to normally using the ActionScript Number type, which maybe has a decimal rather than binary floating point - anyway, it would never give this output.