如何在字符串中的某个位置插入一个字符?
我收到一个带有 6 位数字值的 int
。我想将其显示为 String
,在距 int
末尾 2 位处有一个小数点 (.)。我想使用 float
但建议使用 String
以获得更好的显示输出(而不是 1234.5
将是 1234.50
代码>)。因此,我需要一个函数,它将接受 int
作为参数,并返回格式正确的 String
,小数点距末尾 2 位。
说:
int j= 123456
Integer.toString(j);
//processing...
//output : 1234.56
I'm getting in an int
with a 6 digit value. I want to display it as a String
with a decimal point (.) at 2 digits from the end of int
. I wanted to use a float
but was suggested to use String
for a better display output (instead of 1234.5
will be 1234.50
). Therefore, I need a function that will take an int
as parameter and return the properly formatted String
with a decimal point 2 digits from the end.
Say:
int j= 123456
Integer.toString(j);
//processing...
//output : 1234.56
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
正如评论中提到的, StringBuilder可能比使用 StringBuffer 更快的实现。正如 Java 文档中提到的:
用法:
或者如果您需要同步,请使用 StringBuffer 具有类似的用法:
As mentioned in comments, a StringBuilder is probably a faster implementation than using a StringBuffer. As mentioned in the Java docs:
Usage :
Or if you need synchronization use the StringBuffer with similar usage :
使用 ApacheCommons3 StringUtils,你也可以做
它基本上是子字符串连接,但如果你不介意使用库,或者已经依赖于 StringUtils,那么它会更短
Using ApacheCommons3 StringUtils, you could also do
it's basically substring concatenation but shorter if you don't mind using libraries, or already depending on StringUtils
在大多数用例中,使用
StringBuilder
(如已经回答的那样)是实现此目的的好方法。但是,如果性能很重要,这可能是一个不错的选择。In most use-cases, using a
StringBuilder
(as already answered) is a good way to do this. However, if performance matters, this may be a good alternative.对于 Kotlin 伙计们;)来自已接受的答案(@MikeThomsen 的)
测试 ✅
For Kotlin dudes ;) from the accepted answer (@MikeThomsen's)
Test ✅
String.format("%0d.%02d", d / 100, d % 100);
String.format("%0d.%02d", d / 100, d % 100);
您可以使用
根据评论,12345/100.0 会更好,就像使用 double 而不是 float 一样。
You could use
As per the comments, 12345/100.0 would be better, as would the use of double instead of float.
如果您使用的系统中浮点值昂贵(例如没有 FPU)或不允许(例如在会计中),您可以使用类似以下内容:
否则 DecimalFormat 是更好的解决方案。 (上面的 StringBuilder 变体不适用于小数字(<100)
If you are using a system where float is expensive (e.g. no FPU) or not allowed (e.g. in accounting) you could use something like this:
Otherwise the DecimalFormat is the better solution. (the StringBuilder variant above won't work with small numbers (<100)
我认为在某个位置插入字符串的一种更简单、更优雅的解决方案是这样的:
例如,将缺少的
:
插入到时间字符串中:它的作用是匹配 <从字符串开头开始的 code>position 个字符,对其进行分组,并将该组替换为自身 (
$1
),后跟insert
字符串。请注意replaceAll,即使总是出现一次,因为第一个参数必须是正则表达式。当然,它没有与 StringBuilder 解决方案相同的性能,但我相信作为一种简单且易于阅读的单行(与庞大的方法相比)的简洁和优雅足以使其成为大多数非性能的首选解决方案-关键用例。
注意,出于文档原因,我正在解决标题中的一般问题,当然,如果您正在处理十进制数字,您应该使用已经提出的特定于域的解决方案。
I think a simpler and more elegant solution to insert a String in a certain position would be this one-liner:
For example, to insert a missing
:
into a time String:What it does is, matches
position
characters from the beginning of the string, groups that, and replaces the group with itself ($1
) followed by theinsert
string. Mind the replaceAll, even though there's always one occurrence, because the first parameter must be a regex.Of course it does not have the same performance as the StringBuilder solution, but I believe the succinctness and elegance as a simple and easier to read one-liner (compared to a huge method) is sufficient for making it the preferred solution in most non performance-critical use-cases.
Note I'm solving the generic problem in the title for documentation reasons, of course if you are dealing with decimal numbers you should use the domain-specific solutions already proposed.
这里有很好的答案,但是通过添加 Kotlin 扩展,我们可以做得更简单:
此示例显示卡到期日期,斜线 (/) 用于第二个索引。因此,在这种情况下生成的索引将在第二个索引处具有 / 。
如果您想替换而不添加:
There are good answers here, but with Kotlin extensions addition we can do it even more simply:
This example shows a card expiry date, and slash (/) is intended at 2nd Index. So the resulting index in this case will have / at 2nd index.
If you want to replace and not add:
试试这个:
Try this :