-7

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

7..
  • 15
  • 4
  • Hint: Formatter see http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html – JFPicard Nov 24 '15 at 20:39
  • 1
    I'm voting to close this question as off-topic because the OP has expended no effort in a solution him/her self. – KevinDTimm Nov 24 '15 at 20:46
  • WOW it's true what they say, I've asked for help here and your going to try and close my question... – 7.. Nov 24 '15 at 21:39
  • 1
    @Djldthebest At SO, we help people that want to help themselves. When you ask a question like this one (without any apparent effort shown), it gives us the impression that you're trying to get us to do the work for you. In the future, when posting a question, please show what you have already tried; we are much more eager to help you if we can see that you've actually attempted to solve the problem yourself. – Mage Xy Nov 24 '15 at 22:01

1 Answers1

0

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'.

Bimde
  • 722
  • 8
  • 20
  • Thank You. I had used the substring but was struggling with the length of the string. But this has really helped me out. – 7.. Nov 24 '15 at 21:41