如何在WPF窗口中有条件地格式化十进制值?
目前我使用波纹管标记来格式化十进制值,但当值为0.00时它显示00.请帮忙.
<TextBlock
Grid.Column="6"
Padding="2"
Text="{Binding Path=TotalAwardsExpended, StringFormat='{}{0:0,0}'}" />
Run Code Online (Sandbox Code Playgroud) 我希望通过以下方式实现以下目标str.format:
x,y = 1234,5678
print str(x)[2:] + str(y)[:2]
Run Code Online (Sandbox Code Playgroud)
我能做到的唯一方法是:
print '{0}{1}'.format(str(x)[2:],str(y)[:2])
Run Code Online (Sandbox Code Playgroud)
现在,这个例子和我真正拥有的是一个冗长而凌乱的字符串,所以我想把切片放在里面{}.我研究了文档,但我无法弄清楚正确的语法.我的问题是:是否可以在替换字段内切换字符串?
我们知道,格式化一个参数是可以做到用一个 %s在一个字符串:
>>> "Hello %s" % "world"
'Hello world'
Run Code Online (Sandbox Code Playgroud)
对于两个参数,我们可以使用两个%s(duh!):
>>> "Hello %s, %s" % ("John", "Joe")
'Hello John, Joe'
Run Code Online (Sandbox Code Playgroud)
那么,如何格式化可变数量的参数而不必在基本字符串中明确定义一些%s等于格式化参数的数量?如果这样的事情存在,那将是非常酷的:
>>> "Hello <cool_operator_here>" % ("John", "Joe", "Mary")
Hello JohnJoeMary
>>> "Hello <cool_operator_here>" % ("John", "Joe", "Mary", "Rick", "Sophie")
Hello JohnJoeMaryRickSophie
Run Code Online (Sandbox Code Playgroud)
这是否可能,或者我唯一能做的就是做一些事情:
>>> my_args = ["John", "Joe", "Mary"]
>>> my_str = "Hello " + ("".join(["%s"] * len(my_args)))
>>> my_str % tuple(my_args)
"Hello JohnJoeMary"
Run Code Online (Sandbox Code Playgroud)
注意:我需要使用%s字符串格式化运算符.
更新: …
我认为这很简单,但搜索谷歌似乎并没有帮助.
我基本上试图编写一个函数,当有两个整数(例如800和600)时,它会返回一个字符串比例(例如4:3).
string GetRatio(Int A, Int B) {
// Code I'm looking for
return Ratio;
}
Run Code Online (Sandbox Code Playgroud) 我有一个十进制数字,比如1234.500.
我想让它显示1,234.4
我目前正在将其转换为双倍以删除尾随的'0'.
string.Format("{0:0,0}",1234.500)删除小数位,其他格式选项似乎使用两个小数位.
有人能提供见解吗?
格式化字符串时,我的字符串可能包含"%"我不希望转换的模数.我可以逃避串和每一个变化"%",以"%%"作为一种变通方法.
例如,
'Day old bread, 50%% sale %s' % 'today!'
Run Code Online (Sandbox Code Playgroud)
输出:
'Day old bread, 50% sale today'
Run Code Online (Sandbox Code Playgroud)
但有逃避的替代方案吗?我希望使用dict可以使Python忽略任何非关键字转换.
例如,
'Day old bread, 50% sale %(when)s' % {'when': 'today'}
Run Code Online (Sandbox Code Playgroud)
但Python仍然看到第一个模数,%并给出一个:
TypeError: not enough arguments for format string
Run Code Online (Sandbox Code Playgroud) 想要删除所有0放在某个变量的开头.
一些选择:
$var = 0002,我们应该剥离第一000($var = 2)var = 0203410我们应该删除第一个0($var = 203410)var = 20000- 什么都不做($var = 20000)解决办法是什么?
我有以下Python代码(我使用的是Python 2.7.X):
my_csv = '{first},{middle},{last}'
print( my_csv.format( first='John', last='Doe' ) )
Run Code Online (Sandbox Code Playgroud)
我得到一个KeyError例外,因为没有指定'middle'(这是预期的).但是,我希望所有这些占位符都是可选的.如果未指定这些命名参数,我希望删除占位符.所以上面打印的字符串应该是:
John,,Doe
Run Code Online (Sandbox Code Playgroud)
是否有内置功能使这些占位符可选,或者是否需要更深入的工作?如果是后者,如果有人能告诉我最简单的解决方案,我会很感激!
我想在API中提供自动字符串格式,以便:
my_api("path/to/{self.category}/{self.name}", ...)
Run Code Online (Sandbox Code Playgroud)
可以用格式化字符串中调出的属性值替换.
如何从Python格式字符串中提取关键字参数:
"non-keyword {keyword1} {{escaped brackets}} {} {keyword2}" => 'keyword1', 'keyword2'
Run Code Online (Sandbox Code Playgroud) 我正在努力做到以下几点:
>>> x = (1,2)
>>> y = 'hello'
>>> '%d,%d,%s' % (x[0], x[1], y)
'1,2,hello'
Run Code Online (Sandbox Code Playgroud)
但是,我有x两个以上的长项,所以我试过:
>>> '%d,%d,%s' % (*x, y)
Run Code Online (Sandbox Code Playgroud)
但这是语法错误.如果不像第一个例子那样编制索引,这样做的正确方法是什么?
python ×6
string ×4
formatting ×2
.net ×1
binding ×1
c# ×1
php ×1
python-3.x ×1
wpf ×1
zero ×1