Also if I have:
"23" and I want to print that out as 23p
How can I get Java to see if the string has 2 digits that are less than 99(p) or, "234" and see that there are more than 2 digits and convert the "234" to "£2.34"
Thanks
Also if I have:
"23" and I want to print that out as 23p
How can I get Java to see if the string has 2 digits that are less than 99(p) or, "234" and see that there are more than 2 digits and convert the "234" to "£2.34"
Thanks
You could use the length of the string as the determinant of whether to format the string using 'p' or '£', then use .substring()
to get the individual parts of the string (the euros and cents) Here's an example:
if(string.length() < 3) {
System.out.println(string+"p");
} else {
System.out.println("£"+string.substring(0, string.length()-2)+"."+string.substring(string.length-2));
}
What this does is:
If the length of the string is greater than 2, it takes the part before the last 2 digits (using string.substring(0, string.length()-2)
) and the second part (string.substring(string.length-2)
), separates them with a '.'
and precedes everything with a '£', achieving the result '£euros.cents'.