如何在字符串中的某个位置插入一个字符? [英] How to insert a character in a string at a certain position?

查看:54
本文介绍了如何在字符串中的某个位置插入一个字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到一个带有 6 位数值的 int.我想将其显示为 String,并在 int 末尾的 2 位数处带有小数点 (.).我想使用 float 但有人建议使用 String 以获得更好的显示输出(而不是 1234.5 将是 1234.50).因此,我需要一个函数,该函数将一个 int 作为参数并返回格式正确的 String,其中小数点距末尾 2 位.

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.

说:

int j= 123456 
Integer.toString(j); 

//processing...

//output : 1234.56

推荐答案

int j = 123456;
String x = Integer.toString(j);
x = x.substring(0, 4) + "." + x.substring(4, x.length());

这篇关于如何在字符串中的某个位置插入一个字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆