Java String Normalize normalizeDose(String dose)

Here you can find the source of normalizeDose(String dose)

Description

Normalize dose

License

Apache License

Parameter

Parameter Description
field "form" in the output e.g. "1,200 mg"

Return

normalized output in RXNOMR e.g. "1200mg"

Declaration


public static String normalizeDose(String dose) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    /**/*from   w w  w  .  jav a2s. c  om*/
     * Normalize dose
     * 
     * @param field "form" in the output e.g. "1,200 mg"
     * @return normalized output in RXNOMR e.g. "1200mg"
     * @see Util
     */

    public static String normalizeDose(String dose) {

        if (dose.trim().equals(""))
            return "";
        //System.out.println("dose:"+dose);
        dose = dose.replace("milligrams", "mg").replace("milligram", "mg");
        dose = dose.replaceAll("[ ]*\\,[ ]*", "");

        java.text.DecimalFormat df = new java.text.DecimalFormat("##.######");
        //transfer mcg to mg
        Pattern p = Pattern.compile("\\d*\\.?\\d*mcg");
        Matcher m = p.matcher(dose);
        //System.out.println(dose);
        if (m.find()) {
            //if(dose.startsWith("50"))
            //   System.out.println(m.group(0));
            String dose_mcg = m.group(0).substring(0, m.group(0).length() - 3).trim();
            if (isNumeric(dose_mcg)) {

                String dd = df.format(Float.parseFloat(dose_mcg) * 0.001);
                dose = dose.replace(m.group(0), dd + "mg");
            }
            //if(dose.startsWith("50")){
            //   System.out.println(dose_mcg);
            //   System.out.println(norm_dose);
            //}
        }

        /*
        if(dose.matches("-?\\d+(\\.\\d+)?")){
           dose = dose.trim()+"mg";
        }*/

        if (dose.matches("\\d*\\.?\\d*%")) {
            String perc = dose.trim().substring(0, dose.trim().length() - 1);
            if (isNumeric(perc)) {
                //java.text.DecimalFormat df=new java.text.DecimalFormat("##.######");
                return df.format(Float.parseFloat(perc) / 100);

            }
        }
        //System.out.println("dose"+dose);

        return dose;
    }

    public static boolean isNumeric(String str) {
        try {
            double d = Double.parseDouble(str);
        } catch (NumberFormatException nfe) {
            return false;
        }
        return true;
    }
}

Related

  1. normalize(String text)
  2. normalize(String uri)
  3. normalize(String value)
  4. normalize(String value, Locale locale)
  5. normalizeCutter(String cutter, int numDigits)
  6. normalizeEnglishIdentifier(String id)
  7. normalizeFieldNameOrPath(final String nameOrPath)
  8. normalizeIndex(String input, String[] indexList)
  9. normalizeMatchup(final String matchup)