Skip to content

Commit

Permalink
Re-add precision Round
Browse files Browse the repository at this point in the history
  • Loading branch information
ericboucher committed May 14, 2018
1 parent e31e9a5 commit a7bfc7d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
5 changes: 3 additions & 2 deletions frontend/src/components/DownloadAnalysisLink/BuildCSV.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import { averageMeasure, maxMeasure, minMeasure } from '../../utils/analytics'
import { generateCSV } from '../../utils/csv'
import { adequaciesFromServiceArea, representativePointsFromServiceAreas, summaryStatisticsByServiceArea } from '../../utils/data'
import { parseSerializedServiceArea, safeDatasetHint } from '../../utils/formatters'
import { precisionRound } from '../../utils/numbers'
import { snakeCase } from '../../utils/string'

const staticCsvRootUrl: string = CONFIG.staticAssets.rootUrl
const staticCsvPath: string = CONFIG.staticAssets.csv.path

const defaultPrecision: number = 2 // Default decimal precision for CSV.
/**
* Build an analysis CSV for download from the census data.
*/
Expand Down Expand Up @@ -81,7 +82,7 @@ function getDataForCensusCategories(demographics: any, populationByAnalytics: nu
return flattenDeep(
censusCategories.map(_ => {
return CENSUS_MAPPING[_].map(group => {
return (populationByAnalytics.map(category => category * 0.01 * demographics[_][group] || 0))
return (populationByAnalytics.map(category => precisionRound(category * 0.01 * demographics[_][group] || 0, defaultPrecision)))
})
})
)
Expand Down
8 changes: 8 additions & 0 deletions frontend/src/utils/numbers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,11 @@ export function maybeParseFloat(string: string | null) {
}
return float
}

/**
* Convenience function for rounding without using toFixed (which is for formatting).
*/
export function precisionRound(number: number, precision: number): number {
const factor = Math.pow(10, precision)
return Math.round(number * factor) / factor
}

0 comments on commit a7bfc7d

Please sign in to comment.