Here you can find the source of countFormat(int no)
Parameter | Description |
---|---|
no | The number to format |
public static String countFormat(int no)
//package com.java2s; /******************************************************************************* * Copyright 2009, 2010 Innovation Gate GmbH * /* w w w . ja va2 s. c om*/ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. ******************************************************************************/ import java.text.DecimalFormat; public class Main { /** * Returns a formatted number inclusive counting ending (see {@link #countEnding(int)}. * @param no The number to format * @return The formatted number */ public static String countFormat(int no) { return new DecimalFormat().format(no) + countEnding(no); } /** * Variant of {@link #countFormat(int)} that takes a string that will be parsed to an integer * @param noStr A string that is parsable as integer * @return The formatted number */ public static String countFormat(String noStr) { return countFormat(Integer.parseInt(noStr)); } /** * Returns english counting endings for numbers "st", "nd", "rd" and "th" like in "1st", "2nd", "3rd", "4th", "21st" etc. * @param no The number to determine ending for * @return The ending string exclusive the number */ public static String countEnding(int no) { int remainder = no % 10; if (remainder == 1) { return "st"; } else if (remainder == 2) { return "nd"; } else if (remainder == 3) { return "rd"; } else { return "th"; } } /** * Parses an integer from a string. * Other than the JRE functions this method also copes with integers that are expressed like floats, like 10.0 * @param str A string representing a number. * @return The integer. If the string represented a float the integer part of that is returned */ public static int parseInt(String str) { double dValue = Double.parseDouble(str); return (int) Math.floor(dValue); } }