string-formatting

“IllegalFormatConversionException: d != java.lang.String” when padding number with 0s?

不羁的心 提交于 2019-12-01 14:32:51
问题 I had a perfectly working code yesterday, in the exact form of: int lastRecord = 1; String key = String.format("%08d", Integer.toString(lastRecord)); Which would pad it nicely to 00000001. Now I kicked it up a notch with twoKeyChar getting a string from a table and lastRecord getting an int from a table. As you can see the concept is essentially the same - I convert an int to a string and try to pad it with 0s; however, this time I get the following error: java.util

Convert basic script to Objective C (money formatting)

不打扰是莪最后的温柔 提交于 2019-12-01 13:41:09
I've got this basic like script that I need to convert to objective c, it turns big units of money into shortened versions (ie: 1.2m, etc), I've got most of the conversion done, but the biggest problem I'm having is right at the end. The original basic code is: ; Basic Code Function ShortCash$(BigNumber) out$="" ; First, grab the length of the number L=Len(BigNumber) Letter$="" ;Next, Do a sweep of the values, and cut them down. If l<13 out$=(BigNumber/1000000000) ; For each figure, out remainder should be divided so that it leaves a 2 digit decimal number.. remainder=(BigNumber Mod 1000000000

PHP: formatting time Stackoverflow or Apple Mail-style

无人久伴 提交于 2019-12-01 10:44:56
问题 There is this really nice function from the php.net documentation that enables you to format time in a Facebook-style manner (e.g., 2 minutes ago , 4 weeks ago , or 3 years ago ). However, I prefer the way Stackoverflow and Apple Mail does it which is generally as follows: The current day is listed in x seconds ago or x hours ago or time (e.g, 4:35pm ). Yesterday is listed as "Yesterday". All days after that are listed by M/D/Y. Has anyone adapted this php.net script to do this or might share

How can i use f-string with a variable, not with a string literal?

北城余情 提交于 2019-12-01 10:41:22
I want to use f-string with my string variable, not with string defined with a string literal, "..." . here is my code name=["deep","mahesh","nirbhay"] user_input = r"certi_{element}" # this string i ask from user for element in name: print(f"{user_input}") This code gives output: certi_{element} certi_{element} certi_{element} But I want: certi_{deep} certi_{mahesh} certi_{nirbhay} how can I do this? f"..." strings are great when interpolating expression results into a literal , but you don't have a literal, you have a template string in a separate variable. You can use str.format() to apply

Format currency without rounding

自作多情 提交于 2019-12-01 10:37:45
I have the need to format a decimal number as currency but I do not wish for any rounding to occur in the process. For example (example culture is en-US) Dim money = 1234.556789D money.ToString("C") ' Yields $1,234.56 (notice the rounding & truncating) money.ToString("C99") ' Yields $1,234.556789000000000000....0 -- close but not perfect, those trailing zeros are unwanted. ' I want something that would result in this... money.ToString("??") ' Yields $1,234.56789 ' likewise.... money = 0.1D money.ToString("??") ' Yields $0.10 (notice that it at least matches the culture's currency format -- two

Pad or truncate string based on fixed length

微笑、不失礼 提交于 2019-12-01 08:12:19
Currently have code that looks something like; print '{: <5}'.format('test') This will pad my string with ' ' if it is less than 5 characters. If the string is more than 5 characters, I'd need the string to be truncated. Without explicitly checking the length of my string before formatting it, is there a better way to pad if less than fixed length or truncate if greater than fixed length? You can use 5.5 to combine truncating and padding so that the output will always be of length of five: '{:5.5}'.format('testsdf') # 'tests' '{:5.5}'.format('test') # 'test ' You could use str.ljust and slice

Format currency without rounding

六月ゝ 毕业季﹏ 提交于 2019-12-01 08:07:41
问题 I have the need to format a decimal number as currency but I do not wish for any rounding to occur in the process. For example (example culture is en-US) Dim money = 1234.556789D money.ToString("C") ' Yields $1,234.56 (notice the rounding & truncating) money.ToString("C99") ' Yields $1,234.556789000000000000....0 -- close but not perfect, those trailing zeros are unwanted. ' I want something that would result in this... money.ToString("??") ' Yields $1,234.56789 ' likewise.... money = 0.1D

Python print out float or integer

橙三吉。 提交于 2019-12-01 06:30:59
问题 How can i print out float if the result have decimal or print out integer if the result have no decimal? c = input("Enter the total cost of purchase: ") bank = raw_input("Enter the bank of your credit card (DBS, OCBC, etc.): ") dbs1 = ((c/float(100))*10) dbs2 = c-dbs1 ocbc1 = ((c/float(100))*15) ocbc2 = c-ocbc1 if (c > 200): if (bank == 'DBS'): print('Please pay $'+str(dbs2)) elif (bank == 'OCBC'): print('Please pay $'+str(ocbc2)) else: print('Please pay $'+str(c)) else: print('Please pay $'

R: Converting “special” letters into UTF-8?

柔情痞子 提交于 2019-12-01 06:11:12
问题 I run into problems matching tables where one dataframe contains special characters and the other doesn't. Example: Do ñ a Ana County vs. Dona Ana County Here is a script where you can reproduce the outputs: library(tidyverse) library(acs) tbl_df(acs::fips.place) # contains "Do\xf1a Ana County" tbl_df(tigris::fips_codes) # contains "Dona Ana County" Example: tbl_df(tigris::fips_codes) %>% filter(county == "Dona Ana County") returns: # A tibble: 1 x 5 state state_code state_name county_code

Modifying the Parameters of a TextBox's Text Binding through the use of a Style

主宰稳场 提交于 2019-12-01 06:03:39
问题 I would like to have a TextBox that displays a number in currency format (by setting StringFormat=c on the binding). When the TextBox is selected (when IsKeyboardFocused==true ), I would like the formatting to go away, until the focus on the TextBox is lost. I found a way to do this, code pasted below. My problem with this is that the binding is specified inside the Style - this means I have to retype the style for every TextBox I want to do this for. Idealy I would like to put the style