Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class trening {
- public static void main(String[] args) {
- System.out.println(trening.strToInt("12e5"));
- }
- public static int strToInt(String str) {
- StringBuilder resultsInString = new StringBuilder();
- Integer results = 0;
- int ascii = 0;
- int iMultiplier = 1;
- try {
- Character odczyt = str.charAt(0);
- ascii = odczyt;
- if (ascii == 45) {
- for (int i = 1; i < str.length(); i++) {
- odczyt = str.charAt(i);
- ascii = odczyt;
- if (ascii > 47 && ascii < 58) {
- resultsInString.append(odczyt);
- } else if (ascii == 69 || ascii == 101) {
- odczyt = str.charAt(++i);
- ascii = odczyt;
- if (ascii > 47 && ascii < 58)
- iMultiplier = (int) Math.pow(10, Double.parseDouble(odczyt.toString()));
- break;
- } else {
- break;
- }
- }
- int finalResults = Integer.parseInt(resultsInString.toString());
- results = finalResults * iMultiplier * -1;
- } else if (ascii > 47 && ascii < 58) {
- odczyt = str.charAt(0);
- ascii = odczyt;
- resultsInString.append(odczyt);
- for (int i = 1; i < str.length(); i++) {
- odczyt = str.charAt(i);
- ascii = odczyt;
- if (ascii > 47 && ascii < 58) {
- resultsInString.append(odczyt);
- } else if (ascii == 69 || ascii == 101) {
- odczyt = str.charAt(++i);
- ascii = odczyt;
- if (ascii > 47 && ascii < 58)
- iMultiplier = (int) Math.pow(10, Double.parseDouble(odczyt.toString()));
- break;
- } else {
- break;
- }
- }
- int finalResults = Integer.parseInt(resultsInString.toString());
- results = finalResults * iMultiplier;
- }
- else if (ascii == 43) {
- for (int i = 1; i < str.length(); i++) {
- odczyt = str.charAt(i);
- ascii = odczyt;
- if (ascii > 47 && ascii < 58) {
- resultsInString.append(odczyt);
- } else if (ascii == 69 || ascii == 101) {
- odczyt = str.charAt(++i);
- ascii = odczyt;
- if (ascii > 47 && ascii < 58)
- iMultiplier = (int) Math.pow(10, Double.parseDouble(odczyt.toString()));
- break;
- } else {
- break;
- }
- }
- int finalResults = Integer.parseInt(resultsInString.toString());
- results = finalResults * iMultiplier;
- }
- } catch (NumberFormatException e) {
- results = 0;
- }
- return results;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement