Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

public class Main {
    public static final int NUM_AFTER_COM = 3;

    public static String calculateAverage(int sum, int count) {
        int temp = sum;
        int countI = count;
        if (temp == 0 || countI == 0)
            return "0";
        temp = temp / countI;
        return setPoint(String.valueOf(temp));
    }

    public static String setPoint(String in) {
        char[] res;
        char[] cur = in.toCharArray();
        boolean isNeg = in.contains("-");
        int lengthNum;
        if (isNeg)
            lengthNum = in.length() - 1;
        else
            lengthNum = in.length();
        int length = in.length() - NUM_AFTER_COM;
        if (lengthNum > NUM_AFTER_COM) {
            res = new char[in.length() + 1];
            res[length] = '.';
            System.arraycopy(cur, 0, res, 0, length);
            System.arraycopy(cur, length, res, length + 1, NUM_AFTER_COM);
        } else {
            int size;
            if (isNeg) {
                length = in.length() - 1;
                size = NUM_AFTER_COM + 3;
                res = new char[size];
                res[0] = '-';
                res[1] = '0';
                res[2] = '.';
                System.arraycopy(cur, 1, res, size - length, length);
            } else {
                length = in.length();
                size = NUM_AFTER_COM + 2;
                res = new char[size];
                res[0] = '0';
                res[1] = '.';
                System.arraycopy(cur, 0, res, size - length, length);
            }
            for (int i = 0; i < res.length; i++) {
                if (res[i] == '\u0000')
                    res[i] = '0';
            }
        }
        return new String(res);
    }
}