例如,1, 2, 128, 256输出可以是(16位):
0000000000000001
0000000000000010
0000000010000000
0000000100000000
Run Code Online (Sandbox Code Playgroud)
我试过了
String.format("%16s", Integer.toBinaryString(1));
Run Code Online (Sandbox Code Playgroud)
它为左边填充放置空格:
` 1'
Run Code Online (Sandbox Code Playgroud)
如何将0s用于填充.我在Formatter中找不到它.还有另一种方法吗?
提前致谢.
PS 这篇文章描述了如何使用左0填充格式化整数,但它不适用于二进制表示.
我们有一个旧的SQL表,SQL Server 2000使用了近10年.
在这里面,我们的员工徽章号码存储为char(6)从000001到999999.
我现在正在编写一个Web应用程序,我需要存储员工徽章编号.
在我的新表,我可以走捷径,并复制旧表,但是我希望有更好的数据传输,更小的尺寸等,通过简单地存储int从价值观1到999999.
在C#中,我可以使用快速格式化int徽章编号的值
public static string GetBadgeString(int badgeNum) {
return string.Format("{0:000000}", badgeNum);
// alternate
// return string.Format("{0:d6}", badgeNum);
}
Run Code Online (Sandbox Code Playgroud)
我如何修改这个简单的SQL查询以格式化返回的值?
SELECT EmployeeID
FROM dbo.RequestItems
WHERE ID=0
Run Code Online (Sandbox Code Playgroud)
如果EmployeeID是7135,则应返回此查询007135.
自升级到最新的Xcode 3.2.1和Snow Leopard以来,我一直在收到警告
"格式不是字符串文字,没有格式参数"
来自以下代码:
NSError *error = nil;
if (![self.managedObjectContext save:&error])
{
NSLog([NSString stringWithFormat:@"%@ %@, %@",
errorMsgFormat,
error,
[error userInfo]]);
}
Run Code Online (Sandbox Code Playgroud)
如果errorMsgFormat是NSString格式说明符(例如"print me like this: %@":),上面的NSLog调用有什么问题?什么是修复它的建议方法,以便不生成警告?
是否可以使用高级字符串格式化方法进行部分字符串格式化,类似于字符串模板safe_substitute()函数?
例如:
s = '{foo} {bar}'
s.format(foo='FOO') #Problem: raises KeyError 'bar'
Run Code Online (Sandbox Code Playgroud) 我需要找出如何将数字格式化为字符串.我的代码在这里:
return str(hours)+":"+str(minutes)+":"+str(seconds)+" "+ampm
Run Code Online (Sandbox Code Playgroud)
小时和分钟是整数,秒是浮点数.str()函数将所有这些数字转换为十分之一(0.1)的位置.因此,不是我的字符串输出"5:30:59.07 pm",它将显示类似"5.0:30.0:59.1 pm"的内容.
最重要的是,我需要为我做什么库/功能?
我有ABC123EFFF.
我想拥有001010101111000001001000111110111111111111(即二进制代表,例如42位数和前导零).
怎么样?
Python至少有六种格式化字符串的方法:
In [1]: world = "Earth"
# method 1a
In [2]: "Hello, %s" % world
Out[2]: 'Hello, Earth'
# method 1b
In [3]: "Hello, %(planet)s" % {"planet": world}
Out[3]: 'Hello, Earth'
# method 2a
In [4]: "Hello, {0}".format(world)
Out[4]: 'Hello, Earth'
# method 2b
In [5]: "Hello, {planet}".format(planet=world)
Out[5]: 'Hello, Earth'
# method 2c
In [6]: f"Hello, {world}"
Out[6]: 'Hello, Earth'
In [7]: from string import Template
# method 3
In [8]: Template("Hello, $planet").substitute(planet=world)
Out[8]: 'Hello, Earth'
Run Code Online (Sandbox Code Playgroud)
不同方法的简要历史:
printf自Pythons婴儿期以来,风格格式一直存在 …python printf string-formatting backwards-compatibility deprecated
我s在Python 2.6.5中构造了一个字符串,它将具有不同数量的%s标记,这些标记与列表中的条目数相匹配x.我需要写出一个格式化的字符串.以下不起作用,但表明我正在尝试做什么.在此示例中,有三个%s令牌,列表有三个令牌.
s = '%s BLAH %s FOO %s BAR'
x = ['1', '2', '3']
print s % (x)
Run Code Online (Sandbox Code Playgroud)
我想输出字符串是:
1 BLAH 2 FOO 3 BAR
我试图断言两个System.Drawing.Size结构的相等性,我得到一个格式异常而不是预期的断言失败.
[TestMethod]
public void AssertStructs()
{
var struct1 = new Size(0, 0);
var struct2 = new Size(1, 1);
//This throws a format exception, "System.FormatException: Input string was not in a correct format."
Assert.AreEqual(struct1, struct2, "Failed. Expected {0}, actually it is {1}", struct1, struct2);
//This assert fails properly, "Failed. Expected {Width=0, Height=0}, actually it is {Width=1, Height=1}".
Assert.AreEqual(struct1, struct2, "Failed. Expected " + struct1 + ", actually it is " + struct2);
}
Run Code Online (Sandbox Code Playgroud)
这是预期的行为吗?我在这里做错了吗?
我正在尝试格式化绑定到TimeSpan属性的文本块.如果属性是类型DateTime但它失败,如果它是a,它可以工作TimeSpan.我可以使用转换器完成它.但我试图找出是否有其他选择.
示例代码:
public TimeSpan MyTime { get; set; }
public Window2()
{
InitializeComponent();
MyTime = DateTime.Now.TimeOfDay;
DataContext = this;
}
Run Code Online (Sandbox Code Playgroud)
XAML
<TextBlock Text="{Binding MyTime,StringFormat=HH:mm}"/>
Run Code Online (Sandbox Code Playgroud)
我希望文本块只显示小时和分钟.但它显示为:
19:10:46.8048860
python ×5
binary ×2
c# ×2
string ×2
data-binding ×1
deprecated ×1
formatting ×1
hex ×1
java ×1
list ×1
mstest ×1
nslog ×1
objective-c ×1
printf ×1
sql ×1
sql-server ×1
t-sql ×1
unit-testing ×1
warnings ×1
wpf ×1
xaml ×1