Formatting in percent by Intl.NumberFormat in JavaScript

Because 25.1 is 2510%. Percentages are fractions of 100. If you had 100% it would be 100/100 which is equal to 1. So 25.1% is 25/100 or 0.251 not 25.1 var discount = 0.251; var option = { style: ‘percent’, minimumFractionDigits: 2, maximumFractionDigits: 2 }; var formatter = new Intl.NumberFormat(“en-US”, option); var discountFormat = formatter.format(discount); … Read more

Dollar Sign with Thousands Comma Tick Labels

You can use StrMethodFormatter, which uses the str.format() specification mini-language. import numpy as np import pandas as pd import matplotlib.pyplot as plt import matplotlib.ticker as mtick df = pd.DataFrame({‘A’: [‘A’, ‘B’], ‘B’: [1000,2000]}) fig, ax = plt.subplots(1, 1, figsize=(2, 2)) df.plot(kind=’bar’, x=’A’, y=’B’, align=’center’, width=.5, edgecolor=”none”, color=”grey”, ax=ax) fmt=”${x:,.0f}” tick = mtick.StrMethodFormatter(fmt) ax.yaxis.set_major_formatter(tick) plt.xticks(rotation=25) plt.show()

Format negative amount of USD with a minus sign, not brackets (Java)

It requires a little tweaking of the DecimalFormat returned by NumberFormat.getCurrencyInstance() to do it in a locale-independent manner. Here’s what I did (tested on Android): DecimalFormat formatter = (DecimalFormat)NumberFormat.getCurrencyInstance(); String symbol = formatter.getCurrency().getSymbol(); formatter.setNegativePrefix(symbol+”-“); // or “-“+symbol if that’s what you need formatter.setNegativeSuffix(“”); IIRC, Currency.getSymbol() may not return a value for all locales for all … Read more

Dart – NumberFormat

Actually, I think it’s easier to go with truncateToDouble() and toStringAsFixed() and not use NumberFormat at all: n.toStringAsFixed(n.truncateToDouble() == n ? 0 : 2); So for example: main() { double n1 = 15.00; double n2 = 15.50; print(format(n1)); print(format(n2)); } String format(double n) { return n.toStringAsFixed(n.truncateToDouble() == n ? 0 : 2); } Prints to … Read more

How to turn a float number like 293.4662543 into 293.47 in python?

From The Floating-Point Guide’s Python cheat sheet: “%.2f” % 1.2399 # returns “1.24” “%.3f” % 1.2399 # returns “1.240” “%.2f” % 1.2 # returns “1.20” Using round() is the wrong thing to do, because floats are binary fractions which cannot represent decimal digits accurately. If you need to do calculations with decimal digits, use the … Read more