roundToNearest

roundToNearest

  • (precision: Precision) => (aNumber: number) => number
  1. Round a number to a precision

    const roundToTenth = roundToNearest('0.1')
    roundToTenth(1.55) // is 1.6
    roundToTenth(1.54) // is 1.5
    
    const roundToHundreds = roundToNearest('100')
    roundToHundreds(1250) // is 1300
    roundToHundreds(1249) // is 1200
    const roundToTenth = roundToNearest('0.1')
    roundToTenth(1.55) // is 1.6
    roundToTenth(1.54) // is 1.5
    
    const roundToHundreds = roundToNearest('100')
    roundToHundreds(1250) // is 1300
    roundToHundreds(1249) // is 1200
    
    const roundToTenth = roundToNearest('0.1')
    roundToTenth(1.55) // is 1.6
    roundToTenth(1.54) // is 1.5
    
    const roundToHundreds = roundToNearest('100')
    roundToHundreds(1250) // is 1300
    roundToHundreds(1249) // is 1200
    const roundToTenth = roundToNearest('0.1')
    roundToTenth(1.55) // is 1.6
    roundToTenth(1.54) // is 1.5
    
    const roundToHundreds = roundToNearest('100')
    roundToHundreds(1250) // is 1300
    roundToHundreds(1249) // is 1200
    
  2. We often need to round numbers to specific precisions. Below, we have a few 100-meter track times. Official times need to be reported in hundredths, so let's round them.

    const results = [
      { name: 'sarah', time: 15.926 },
      { name: 'matt', time: 16.143 },
      { name: 'jason', time: 15.907 },
    ]
    
    const roundToHundredth = roundToNearest('0.01')
    const roundTime = result => ({
      ...result,
      time: roundToHundredth(result.time),
    })
    const roundResults = mapValues(roundTime)
    const roundedResults = roundResults(results)
    
    console.log(roundedResults)
    // is [
    //   { name: sarah, time: 15.93 }
    //   { name: matt, time: 16.14 }
    //   { name: jason, time: 15.91 }
    // ]
    type Result = {
      name: string
      time: number
    }
    const results: Result[] = [
      { name: 'sarah', time: 15.926 },
      { name: 'matt', time: 16.143 },
      { name: 'jason', time: 15.907 },
    ]
    
    const roundToHundredth = roundToNearest('0.01')
    const roundTime = (result: Result) => ({
      ...result,
      time: roundToHundredth(result.time),
    })
    const roundResults = mapValues(roundTime)<Result[]>
    const roundedResults = roundResults(results)
    
    console.log(roundedResults)
    // is [
    //   { name: sarah, time: 15.93 }
    //   { name: matt, time: 16.14 }
    //   { name: jason, time: 15.91 }
    // ]
    
    const results = [
      { name: 'sarah', time: 15.926 },
      { name: 'matt', time: 16.143 },
      { name: 'jason', time: 15.907 },
    ]
    
    const roundToHundredth = roundToNearest('0.01')
    const roundTime = result => ({
      ...result,
      time: roundToHundredth(result.time),
    })
    const roundResults = mapValues(roundTime)
    const roundedResults = roundResults(results)
    
    console.log(roundedResults)
    // is [
    //   { name: sarah, time: 15.93 }
    //   { name: matt, time: 16.14 }
    //   { name: jason, time: 15.91 }
    // ]
    type Result = {
      name: string
      time: number
    }
    const results: Result[] = [
      { name: 'sarah', time: 15.926 },
      { name: 'matt', time: 16.143 },
      { name: 'jason', time: 15.907 },
    ]
    
    const roundToHundredth = roundToNearest('0.01')
    const roundTime = (result: Result) => ({
      ...result,
      time: roundToHundredth(result.time),
    })
    const roundResults = mapValues(roundTime)<
      Result[]
    >
    const roundedResults = roundResults(results)
    
    console.log(roundedResults)
    // is [
    //   { name: sarah, time: 15.93 }
    //   { name: matt, time: 16.14 }
    //   { name: jason, time: 15.91 }
    // ]
    
  3. The precision is validated. Below shows the errors you'll receive with bad input.

    try {
      roundToNearest('2')
    } catch (err) {
      show(err)
      // prints
      // Without a decimal, roundToNearest requires a precision passing the regex
      // /^10{0,9}$/
    }
    
    try {
      roundToNearest('0.2')
    } catch (err) {
      show('\n', err)
      // prints
      // With a decimal, roundToNearest requires a precision passing the regex
      // /^0\.0{0,9}1$/
    }
    // incorrect format presents a type error
    roundToNearest('2')
    
    try {
      roundToNearest('2')
    } catch (err) {
      show(err)
      // prints
      // Without a decimal, roundToNearest requires a
      // precision passing the regex /^10{0,9}$/
    }
    
    try {
      roundToNearest('0.2')
    } catch (err) {
      show('\n', err)
      // prints
      // With a decimal, roundToNearest requires a
      // precision passing the regex /^0\.0{0,9}1$/
    }
    // incorrect format presents a type error
    roundToNearest('2')