'Adding dot between two characters in java string
I have a string:
String x = "10";
Now I want to add . between the numbers and print it like this
1.0
How can I achieve this?
Solution 1:[1]
Use the DecimalFormat class to better decouple the value and its representation.
Solution 2:[2]
If the String is always a 2-digit number :
String result = x.charAt(0) + "." + x.charAt(1);
Solution 3:[3]
this function can accepts all number and returns a dotted string, with small touches it can accepts many in other types of data an return data with options
fun getStringWithDot(nums: Int): String {
var str = ""
val istr = nums.toString()
for (ch in istr.indices){
if (ch == istr.length-1){
str += istr.substring(0,1)
}else{
str += istr.substring(ch,ch+1) + "."
}
}
return str
}
Solution 4:[4]
public static void main(String args[])
{
Scanner S=new Scanner(System.in);
int arr[];
int number;
number=S.nextInt();
int length=Integer.toString(number).length();
arr=new int[length];
char arr2[];
arr2=new char[length];
int count=0;
char arr3[];
arr3=new char[length];
String Q=Integer.toString(number);
for(int i=0;i<arr3.length;i++)
{
System.out.print(Q.charAt(i)+".");
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | user1329572 |
| Solution 2 | |
| Solution 3 | KelimeSoft |
| Solution 4 | Daril |
