formatting

How do I format a Decimal to a programatically controlled number of decimals in c#?

孤街醉人 提交于 2019-12-17 14:02:15
问题 How can I format a number to a fixed number of decimal places (keep trailing zeroes) where the number of places is specified by a variable? e.g. int x = 3; Console.WriteLine(Math.Round(1.2345M, x)); // 1.234 (good) Console.WriteLine(Math.Round(1M, x)); // 1 (would like 1.000) Console.WriteLine(Math.Round(1.2M, x)); // 1.2 (would like 1.200) Note that since I want to control the number of places programatically, this string.Format won't work (surely I ought not generate the format string):

Show padding zeros using DecimalFormat

醉酒当歌 提交于 2019-12-17 10:52:24
问题 I'm using DecimalFormat to format doubles to 2 decimal places like this: DecimalFormat dec = new DecimalFormat("#.##"); double rawPercent = ( (double)(count.getCount().intValue()) / (double)(total.intValue()) ) * 100.00; double percentage = Double.valueOf(dec.format(rawPercent)); It works, but if i have a number like 20, it gives me this: 20.0 and I want this: 20.00 Any suggestions? 回答1: The DecimalFormat class is for transforming a decimal numeric value into a String. In your example, you

Proper currency format when not displaying the native currency of a culture

痞子三分冷 提交于 2019-12-17 10:35:37
问题 What is the proper way to format currency if you are formatting a currency that is not the native currency of the current culture? For example, if I am formatting US Dollars for a fr-FR culture do I format it like a en-US culture ( $1,000.00 ) or as an fr-FR culture but changing the Euro symbol to a US Dollar symbol ( 1 000,00 $ ). Perhaps something else ( $1 000,00 or 1 000,00 USD )? 回答1: There's no absolute rules here but a couple of guiding principles: Try and use the number format for

Format Timestamp in outgoing JSON in Golang?

*爱你&永不变心* 提交于 2019-12-17 10:18:26
问题 I've been playing with Go recently and it's awesome. The thing I can't seem to figure out (after looking through documentation and blog posts) is how to get the time.Time type to format into whatever format I'd like when it's encoded by json.NewEncoder.Encode Here's a minimal Code example: package main type Document struct { Name string Content string Stamp time.Time Author string } func sendResponse(data interface{}, w http.ResponseWriter, r * http.Request){ _, err := json.Marshal(data) j :=

Python datetime formatting without zero-padding

萝らか妹 提交于 2019-12-17 10:16:27
问题 Is there a format for printing Python datetimes that won't use zero-padding on dates and times? Format I'm using now: mydatetime.strftime('%m/%d/%Y %I:%M%p') Result: 02/29/2012 05:03PM Desired: 2/29/2012 5:03PM What format would represent the month as '2' instead of '02', and time as '5:03PM' instead of '05:03PM' 回答1: The formatting options available with datetime.strftime() will all zero-pad. You could of course roll you own formatting function, but the easiest solution in this case might be

format a number with commas and decimals in C# (asp.net MVC3)

廉价感情. 提交于 2019-12-17 09:20:08
问题 I Need to display a number with commas and decimal point. Eg: Case 1 : Decimal number is 432324 (This does not have commas or decimal Points) Need to display it as 432,324.00 but not 432,324 Case 2 : Decimal number is 2222222.22 (This does not have commas) Need to display it as 2,222,222.22 I tried ToString("#,##0.##") . But It is Not Formatting it 回答1: int number = 1234567890; Convert.ToDecimal(number).ToString("#,##0.00"); You will get the result 1,234,567,890.00 . 回答2: Maybe you simply

Format a BigDecimal as String with max 2 decimal digits, removing 0 on decimal part

与世无争的帅哥 提交于 2019-12-17 08:30:39
问题 I have a BigDecimal number and i consider only 2 decimal places of it so i truncate it using: bd = bd.setScale(2, BigDecimal.ROUND_DOWN) Now I want to print it as String but removing the decimal part if it is 0, for example: 1.00 -> 1 1.50 -> 1.5 1.99 -> 1.99 I tried using a Formatter, formatter.format but i always get the 2 decimal digits. How can I do this? Maybe working on the string from bd.toPlainString()? 回答1: I used DecimalFormat for formatting the BigDecimal instead of formatting the

Pad left or right with string.format (not padleft or padright) with arbitrary string

旧时模样 提交于 2019-12-17 08:29:09
问题 Can I use String.Format() to pad a certain string with arbitrary characters? Console.WriteLine("->{0,18}<-", "hello"); Console.WriteLine("->{0,-18}<-", "hello"); returns -> hello<- ->hello <- I now want the spaces to be an arbitrary character. The reason I cannot do it with padLeft or padRight is because I want to be able to construct the format string at a different place/time then the formatting is actually executed. --EDIT-- Seen that there doesn't seem to be an existing solution to my

Print out elements from an array with a comma between elements except the last word

∥☆過路亽.° 提交于 2019-12-17 07:33:12
问题 I am printing out elements from a array list, I want to have a comma between each word except the last word. Right now I am doing like this: for (String s : arrayListWords) { System.out.print(s + ", "); } As you understand it will print out the words like this: "one, two, three, four," and the problem is the last comma, how do I solve this? All answers appreciated! Best regards, Erica 回答1: Print the first word on its own if it exists. Then print the pattern as comma first, then the next

JPanel Padding in Java

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-17 07:14:12
问题 I have a formatting question for my Java swing application. It should be fairly straightforward, but I am having difficulty finding any aid (Every topic seems to be regarding removing any default padding in JPanel). The text in my various JPanels hug the sides and top, touching the colored borders: how can I add padding? Thank you. 回答1: Set an EmptyBorder around your JPanel . Example: JPanel p =new JPanel(); p.setBorder(new EmptyBorder(10, 10, 10, 10)); 回答2: When you need padding inside the