number-formatting

Formatting Decimal Number

最后都变了- 提交于 2020-01-25 06:13:47
问题 I am formatting a decimal number and I have the following criteria to format it: Number should be at most two decimal places ( 10.1234=>10.12 ) If there is only one digit after decimal then it will ends up with an extra 0 ( 10.5=>10.50 ) Thousand separator will be comma ( 12345.2345 => 12,345.23 ) I have written following logic: double x = Double.parseDouble(value.toString()); String dec = x % 1 == 0 ? new java.text.DecimalFormat("###,###.##").format(x) : new java.text.DecimalFormat("###,###

Formatting Decimal Number

让人想犯罪 __ 提交于 2020-01-25 06:13:24
问题 I am formatting a decimal number and I have the following criteria to format it: Number should be at most two decimal places ( 10.1234=>10.12 ) If there is only one digit after decimal then it will ends up with an extra 0 ( 10.5=>10.50 ) Thousand separator will be comma ( 12345.2345 => 12,345.23 ) I have written following logic: double x = Double.parseDouble(value.toString()); String dec = x % 1 == 0 ? new java.text.DecimalFormat("###,###.##").format(x) : new java.text.DecimalFormat("###,###

f-string with float formatting in list-comprehension

£可爱£侵袭症+ 提交于 2020-01-24 08:59:25
问题 The [f'str'] for string formatting was recently introduced in python 3.6. link. I'm trying to compare the .format() and f'{expr} methods. f ' <text> { <expression> <optional !s, !r, or !a> <optional : format specifier> } <text> ... ' Below is a list comprehension that converts Fahrenheit to Celsius. Using the .format() method it prints the results as float to two decimal points and adds the string Celsius: Fahrenheit = [32, 60, 102] F_to_C = ['{:.2f} Celsius'.format((x - 32) * (5/9)) for x in

Format number with JSTL and no rounding

半城伤御伤魂 提交于 2020-01-24 01:28:16
问题 I need to format number using <fmt:formatNumber/> jstl tag. The output should be restricted to 3 places after the decimal point, but rounding isn't allowed, so using the attribute maxFractionDigits="3" isn't appropriate, cause it rounds the number. Do you have any suggestions? 回答1: You could subtract 0.0005 from the number before formatting it. That way the rounding will be equivalent to truncating the original number to 3 decimal places. <fmt:formatNumber value="${myNumber - 0.0005}"

How can I round to a certain floating-point precision?

▼魔方 西西 提交于 2020-01-23 11:23:44
问题 I think it's a simple question. I want: a = 1.154648126486416; to become: a = 1.154; and not: a = 1.15000000000; How do I do that without using format('bank') . 回答1: You could do this: a = floor(a*1000)/1000; 回答2: Building on @gnovice's answer, you can format the output as a string to get rid of the extra zeros. See the sprintf documentation for all the formatting options. str=sprintf('The result is %1.3f.',a); disp(str) will show "The result is 1.154." in the command prompt. Or write the

Java: double: how to ALWAYS show two decimal digits

我的梦境 提交于 2020-01-22 08:27:28
问题 I use double values in my project and i would like to always show the first two decimal digits, even if them are zeros. I use this function for rounding and if the value I print is 3.47233322 it (correctly) prints 3.47. But when i print, for example, the value 2 it prints 2.0 . public static double round(double d) { BigDecimal bd = new BigDecimal(d); bd = bd.setScale(2, BigDecimal.ROUND_HALF_UP); return bd.doubleValue(); } I want to print 2.00! Is there a way to do this without using Strings?

Java: double: how to ALWAYS show two decimal digits

大憨熊 提交于 2020-01-22 08:27:26
问题 I use double values in my project and i would like to always show the first two decimal digits, even if them are zeros. I use this function for rounding and if the value I print is 3.47233322 it (correctly) prints 3.47. But when i print, for example, the value 2 it prints 2.0 . public static double round(double d) { BigDecimal bd = new BigDecimal(d); bd = bd.setScale(2, BigDecimal.ROUND_HALF_UP); return bd.doubleValue(); } I want to print 2.00! Is there a way to do this without using Strings?

Defining cross-platform money_format function (Linux and Windows)

邮差的信 提交于 2020-01-21 04:50:43
问题 I have read that money_format is not available on windows, and on some Linux distributions (i.e. BSD 4.11 variants). But I want to write cross-platform library using normal function, when available and using this workaround when not, so my library will be able to run on every PHP-based web server. Is there any simple solution to check whether built-in function is available and if not to include the solution from above? 回答1: The function money_format() is only defined if the system has strfmon

How to get a Double value up to 2 Decimal places [duplicate]

醉酒当歌 提交于 2020-01-12 11:04:47
问题 This question already has answers here : Rounding a double value to x number of decimal places in swift (27 answers) Closed 3 years ago . In my app I am dealing with currency which is need to upto two decimal digit , I have used number formatter for this to display up to two decimal place but When I want this double value up like 0.7 to 0.70 , I am unable to to do that , type double is always giving me 0.7 but i want 0.7 to 0.70 回答1: You can simply do this: let pi = 3.14159 let text = String

How to get a Double value up to 2 Decimal places [duplicate]

那年仲夏 提交于 2020-01-12 11:02:50
问题 This question already has answers here : Rounding a double value to x number of decimal places in swift (27 answers) Closed 3 years ago . In my app I am dealing with currency which is need to upto two decimal digit , I have used number formatter for this to display up to two decimal place but When I want this double value up like 0.7 to 0.70 , I am unable to to do that , type double is always giving me 0.7 but i want 0.7 to 0.70 回答1: You can simply do this: let pi = 3.14159 let text = String