All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.saxon.option.local.Numberer_fr Maven / Gradle / Ivy

There is a newer version: 12.5
Show newest version
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2018-2022 Saxonica Limited
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
// If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
// This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

package net.sf.saxon.option.local;

import net.sf.saxon.expr.number.AbstractNumberer;


/**
 * Class Numberer_fr is a number formatter for french. This one will be
 * activated for language="fr"
 *
 * @author Luc Rochefort
 */

public class Numberer_fr extends AbstractNumberer {

    /**
     * Automatically generated serialVersionUID number
     */
    private static final long serialVersionUID = -222104830008011842L;

    private static final String[] frenchUnits = {"", "Un", "Deux", "Trois", "Quatre", "Cinq", "Six", "Sept", "Huit", "Neuf", "Dix", "Onze", "Douze", "Treize", "Quatorze", "Quinze", "Seize", "Dix-sept", "Dix-huit", "Dix-neuf"};

    private static final String[] frenchTens = {"", "Dix", "Vingt", "Trente", "Quarante", "Cinquante", "Soixante", "Soixante", "Quatre-vingt", "Quatre-vingt"};

    private static final String[] frenchOrdinalUnits = {"", "Premier", "Deuxi\u00e8me", "Troisi\u00e8me", "Quatri\u00e8me", "Cinqui\u00e8me", "Sixi\u00e8me", "Septi\u00e8me", "Huiti\u00e8me", "Neuvi\u00e8me", "Dixi\u00e8me", "Onzi\u00e8me", "Douzi\u00e8me", "Treizi\u00e8me", "Quatorzi\u00e8me", "Quinzi\u00e8me", "Seizi\u00e8me", "Dix-septi\u00e8me", "Dix-huiti\u00e8me", "Dix-neuvi\u00e8me"};

    private static final String[] frenchOrdinalTens = {"", "Dixi\u00e8me", "Vingti\u00e8me", "Trenti\u00e8me", "Quaranti\u00e8me", "Cinquanti\u00e8me", "Soixanti\u00e8me", "Soixante", "Quatre-vingti\u00e8me", "Quatre-vingt"};

    private static final String[] frenchDays = {"Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi", "Dimanche"};

    private static final String[] frenchMonths = {"Janvier", "F\u00e9vrier", "Mars", "Avril", "Mai", "Juin", "Juillet", "Ao\u00fbt", "Septembre", "Octobre", "Novembre", "D\u00e9cembre"};

    /*
      * (non-Javadoc)
      *
      * @see net.sf.saxon.expr.number.Numberer_en#ordinalSuffix(java.lang.String,
      *      long)
      */
    /*@NotNull*/
    @Override
    protected String ordinalSuffix(String ordinalParam, long number) {
        if (number != 1) {
            return "e";
        } else {
            return "er";
        }
    }

    /*
      * (non-Javadoc)
      *
      * @see net.sf.saxon.expr.number.Numberer_en#toWords(long)
      */
    @Override
    public String toWords(long number) {
        return toWords(number, true);
    }

    /*
      * (non-Javadoc)
      *
      * @see net.sf.saxon.expr.number.Numberer_en#toWords(long, int)
      */
    @Override
    public String toWords(long number, int wordCase) {
        String s = toWords(number);
        if (wordCase == UPPER_CASE) {
            return s.toUpperCase();
        } else if (wordCase == LOWER_CASE) {
            return s.toLowerCase();
        } else {
            return s;
        }
    }

    private String toWords(long number, boolean terminal) {
        if (number == 0) {
            return "Z\u00e9ro";
        } else if (number >= 1000000000000000000L) {
            long rem = number % 1000000000000000000L;
            long n = number / 1000000000000000000L;
            String s = (n == 1 ? "Un" : toWords(n, true));
            return s + " quintillion" + (n > 1 ? "s" : "") + (rem == 0 ? "" : " " + toWords(rem, LOWER_CASE, terminal));
        } else if (number >= 1000000000000000L) {
            long rem = number % 1000000000000000L;
            long n = number / 1000000000000000L;
            String s = (n == 1 ? "Un" : toWords(n, true));
            return s + " quatrillion" + (n > 1 ? "s" : "") + (rem == 0 ? "" : " " + toWords(rem, LOWER_CASE, terminal));
        } else if (number >= 1000000000000L) {
            long rem = number % 1000000000000L;
            long n = number / 1000000000000L;
            String s = (n == 1 ? "Un" : toWords(n, true));
            return s + " trillion" + (n > 1 ? "s" : "") + (rem == 0 ? "" : " " + toWords(rem, LOWER_CASE, terminal));
        } else if (number >= 1000000000) {
            long rem = number % 1000000000;
            long n = number / 1000000000;
            String s = (n == 1 ? "Un" : toWords(n, true));
            return s + " milliard" + (n > 1 ? "s" : "") + (rem == 0 ? "" : " " + toWords(rem, LOWER_CASE, terminal));
        } else if (number >= 1000000) {
            long rem = number % 1000000;
            long n = number / 1000000;
            String s = (n == 1 ? "Un" : toWords(n, true));
            return s + " million" + (n > 1 ? "s" : "") + (rem == 0 ? "" : " " + toWords(rem, LOWER_CASE, terminal));
        } else if (number >= 1000) {
            long rem = number % 1000;
            long n = number / 1000;
            String s = (n == 1 ? "" : toWords(n, false));
            return s + (n == 1 ? "Mille" : " mille") + (rem == 0 ? "" : " " + toWords(rem, LOWER_CASE, terminal));
        } else if (number >= 100) {
            long rem = number % 100;
            long n = number / 100;
            String s = (n == 1 ? "" : toWords(n, false));
            return s + (n == 1 ? "Cent" : " cent") + (rem == 0 && n > 1 && terminal ? "s" : ((rem != 0) ? " " + toWords(rem, LOWER_CASE, terminal) : ""));
        } else {
            if (number < 20)
                return frenchUnits[(int) number];
            int rem = (int) (number % 10);
            int tens = (int) number / 10;
            if (tens == 7 || tens == 9) {
                rem += 10;
            }
            String link = (rem == 1 || rem == 11) ? ((tens == 8 || tens == 9) ? "-" : " et ") : "-";

            return frenchTens[tens] + (rem == 0 ? ((tens == 8 && terminal) ? "s" : "") : link) + (tens == 0 ? frenchUnits[rem] : frenchUnits[rem].toLowerCase());
        }
    }

    private String toWords(long number, int wordCase, boolean terminal) {
        String s = toWords(number, terminal);
        if (wordCase == UPPER_CASE) {
            return s.toUpperCase();
        } else if (wordCase == LOWER_CASE) {
            return s.toLowerCase();
        } else {
            return s;
        }
    }

    /*
      * (non-Javadoc)
      *
      * @see net.sf.saxon.expr.number.Numberer_en#toOrdinalWords(java.lang.String,
      *      long, int)
      */
    @Override
    public String toOrdinalWords(String ordinalParam, long number, int wordCase) {
        String ord;
        if (number < 20) {
            if (number == 0) {
                ord = "Z\u00e9roi\u00e8me";
            } else {
                ord = frenchOrdinalUnits[(int) number];
            }
        } else if (number < 100) {
            long mod10 = number % 10;
            long int10 = number / 10;
            if (int10 == 7 || int10 == 9) {
                int10 -= 1;
                mod10 += 10;
            }
            if (mod10 == 0) {
                ord = frenchOrdinalTens[(int) int10];
            } else {
                String link = (mod10 == 1 || mod10 == 11) ? ((int10 == 8) ? "-" : " et ") : "-";
                String prefix = toWords(int10 * 10);
                if (int10 == 8) {
                    prefix = prefix.substring(0, prefix.length() - 1);
                }
                String result = prefix + link;
                ord = result + ((mod10 == 1) ? "uni\u00e8me" : toOrdinalWords("", mod10, LOWER_CASE));
            }
        } else {
            String suffix = "i\u00e8me";
            long mod100 = number % 100;
            long int100 = number / 100;
            if (int100 == 70 || int100 == 90) {
                int100 -= 10;
                mod100 += 100;
            }

            String prefix = toWords(int100 * 100, false);
            if (int100 % 10000 == 0) {
                prefix = prefix.replaceFirst("Un ", "");
            }

            /* strip prefix, if needed */
            if ((prefix.endsWith("mille") || prefix.endsWith("Mille")) && mod100 == 0) {
                prefix = prefix.substring(0, prefix.length() - 1);
            } else if (prefix.endsWith("illions") || prefix.endsWith("illiards")) {
                prefix = prefix.substring(0, prefix.length() - 1);
            }

            ord = prefix + ((mod100 == 0) ? suffix : " " + ((mod100 == 1) ? "uni\u00e8me" : toOrdinalWords("", mod100, LOWER_CASE)));
        }
        if (wordCase == UPPER_CASE) {
            return ord.toUpperCase();
        } else if (wordCase == LOWER_CASE) {
            return ord.toLowerCase();
        } else {
            return ord;
        }
    }

    /*
      * (non-Javadoc)
      *
      * @see net.sf.saxon.lib.Numberer#monthName(int, int, int)
      */
    @Override
    public String monthName(int month, int minWidth, int maxWidth) {
        StringBuilder name = new StringBuilder(frenchMonths[month - 1]);
        if (maxWidth < 3) {
            maxWidth = 3;
        }
        if (name.length() > maxWidth) {
            name = new StringBuilder(name.substring(0, maxWidth));
        }
        while (name.length() < minWidth) {
            name.append(" ");
        }
        return name.toString();
    }

    /*
      * (non-Javadoc)
      *
      * @see net.sf.saxon.lib.Numberer#dayName(int, int, int)
      */
    @Override
    public String dayName(int day, int minWidth, int maxWidth) {
        StringBuilder name = new StringBuilder(frenchDays[day - 1]);
        if (maxWidth < 3) {
            maxWidth = 3;
        }
        if (name.length() > maxWidth) {
            name = new StringBuilder(name.substring(0, maxWidth));
        }
        while (name.length() < minWidth) {
            name.append(" ");
        }
        return name.toString();
    }

}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy