Java 中的四舍五入小数

Round decimals in Java

我试图四舍五入一些两位小数,因为除法后我只得到 0.0。 我尝试使用 BigDecimal 方式和 String.format(),但我仍然在以下位置获得 O.00: 关系 Aromatics/Aliphatics:0,00 关系 Heterocyclics/Aliphatics:0,00 关系 Heterocyclics/Aromatic:0,00

我该如何解决?值应该是: 关系Aromatics/Aliphatics:0,14 关系Heterocyclics/Aliphatics:0,12 关系 Heterocyclics/Aromatic:0,875

这是代码:

进口java.io.InputStream; import java.net.URL; import java.util.Set;

import org.biojava.nbio.core.sequence.ProteinSequence; import org.biojava.nbio.core.sequence.compound.AminoAcidCompound; import org.biojava.nbio.core.sequence.io.FastaReaderHelper;

public class BioSeq { ProteinSequence seq = getSequenceForId ("P31574");

    int polarCount = numberOfOccurrences(seq, new HashSet<>(Arrays.asList("Y", "S", "T", "N", "Q", "C","H","R","D","E","K")));
    double polar = ((double)(polarCount));
    
    int NonPolarCount = numberOfOccurrences(seq, new HashSet<>(Arrays.asList("A","V","L","G","I","M","W","P","F")));
    double NonPolar = ((double)(NonPolarCount));
    
    int Aromatic = numberOfOccurrences(seq, new HashSet<>(Arrays.asList("W","Y","F","H")));
    double AromaticCount = ((double)(Aromatic));
    
    int Aliphatic = numberOfOccurrences(seq, new HashSet<>(Arrays.asList("A","V","L","I","P","G","R")));
    double AliphaticCount = ((double)(Aliphatic));
    
    int Heterocyclics = numberOfOccurrences(seq, new HashSet<>(Arrays.asList("W","H","P")));
    double HeteroCount = ((double)(Heterocyclics));
    
    double rel_polarity = (polar/NonPolar); 
    double rel_arom = (Aromatic/Aliphatic);         
    double rel_alip = (Heterocyclics/Aliphatic);    
    double rel_hetero = (Heterocyclics/Aromatic);   
            
    
    System.out.println("Полярные АМК: " + polar);
    System.out.println("Неполярные АМК: " + NonPolar);
    System.out.println("Ароматичесие АМК: " + AromaticCount);
    System.out.println("Алифатические АМК: " + AliphaticCount);
    System.out.println("Гетероциклические АМК: " + HeteroCount);
    System.out.println("_______________________");
    System.out.println("Relations and Percentages: ");
        
    System.out.println("Relation Polar/No polars: " + String.format("%.2f",rel_polarity));
    System.out.println("Relation Aromatics/Aliphatics: " + String.format("%.2f",rel_arom));
    System.out.println("Relation Heterocyclics/Aliphatics: " + String.format("%.2f",rel_alip));
    System.out.println("Relation Heterocyclics/Aromatic: " + String.format("%.2f",rel_hetero));

try {
        System.out.println(getSequenceForId("P31574"));
    } catch (Exception e) {
        e.printStackTrace();
    }
}
private static int numberOfOccurrences(ProteinSequence seq, Set<String> bases) {
    int count = 0;
    for (AminoAcidCompound aminoAcid : seq)
        if(bases.contains(aminoAcid.getBase()))
            count++;
    return count;
}}

double rel_polarity = ((double)polar/(double)NonPolar)*100;

双 rel_arom = ((双)芳香族/(双)脂肪族)*100;

双 rel_alip = ((双)杂环化合物/(双)脂肪族)*100;

双 rel_hetero = ((双)杂环化合物/(双)芳烃)*100;