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

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

问题描述

我得到一个6位数值的 int 。我想把它显示为 String ,从 int 结尾的2位数处加上小数点(。)。我想使用 float ,但建议使用 String 获得更好的显示输出(而不是 1234.5 将会是 1234.50 )。因此,我需要一个函数,它将以 int 作为参数,并返回格式正确的 String

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 paramter 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天全站免登陆