Skip to content

Commit

Permalink
use Intl.NumberFormat
Browse files Browse the repository at this point in the history
  • Loading branch information
leexgh committed Jan 16, 2020
1 parent 5379f50 commit 69b8988
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import * as React from "react";

import {GnomadSummary} from "../../model/GnomadSummary";
import GnomadFrequencyTable, {GnomadTableColumnName} from "./GnomadFrequencyTable";
import { significantDigits } from "../../util/FormatUtils";

const GNOMAD_POPULATION_NAME: {[key:string]: string} = {
"African" : "afr",
Expand Down Expand Up @@ -146,7 +145,11 @@ export default class GnomadFrequency extends React.Component<GnomadFrequencyProp
display = <span>0</span>;
} else {
// show frequency as number with 4 significant digits
display = <span>{significantDigits(result['Total'].alleleFrequency, 4)}</span>;
var significantDigitsFormatter = new Intl.NumberFormat("en", {
minimumSignificantDigits: 1,
maximumSignificantDigits:4
});
display = <span>{significantDigitsFormatter.format(result['Total'].alleleFrequency)}</span>;
}

overlay = () => <GnomadFrequencyTable data={sorted} gnomadUrl={gnomadUrl} />;
Expand Down
13 changes: 0 additions & 13 deletions packages/react-mutation-mapper/src/util/FormatUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,4 @@ export function formatPercentValue(rate: number, fractionDigits: number = 1)

export function numberOfLeadingDecimalZeros(value: number) {
return -Math.floor( (Math.log10(value) / Math.log10(10)) + 1);
}

export function significantDigits(data: number, length: number) {
// data: the original number, length: how many significant digits in return
// refer to: https://stackoverflow.com/questions/27220908
if (data === 0) {
return data;
}
var numDigits = Math.ceil(Math.log10(Math.abs(data)));
var rounded =
Math.round(data * Math.pow(10, length - numDigits)) *
Math.pow(10, numDigits - length);
return rounded.toFixed(Math.max(length - numDigits, 0));
}

0 comments on commit 69b8988

Please sign in to comment.