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

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

问题描述

我进入一个具有6位数字的int.我想将其显示为从int末尾起2位数的小数点(.)的String.我想使用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 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天全站免登陆