title: How to improve my function that rounds floats to nearest 10 if 2 digit number, 100 if 3 digit number etc
body: <p>I am drawing bar charts and i've come across a tricky problem. How to programmatically set the max value for the y axis label depending on the max value for a given series. So if you had a bar with a value of 7, you might want the y axis to go up to 10</p>
  
  <p>My approach is not ideal but works like this:</p>
  
  <ul>
  <li>Get a number to round, like 829</li>
  <li>Count the number of digits (3)</li>
  <li>Use a loop to convert to a string of 0s ("000")</li>
  <li>Add a 1 to the start of the string then convert to a float (1000)</li>
  <li>Find the difference (1000 - 829 = 171)</li>
  <li>Get the first digit of the difference (1) and then add that to the first digit of the float, with the remaining set to zero ("900"), then convert to a number (900)</li>
  </ul>
  
  <p>This means that 725 will see a y axis max label number of 800, and 829 of 900</p>
  
  <p>My code works, but I feel like it's a piece of crap with a hacky approach</p>
  
  <p>I have to code for big numbers. For example, if the float I want to find the max value for is >10000 then take the first two digits, and add 1000 to it. If >100,000 add 10,000</p>
  
  <p><strong>How can I improve here? I'm a little stuck, is my idea of converting to strings even right?!</strong></p>
  
  <p>Full code here:</p>
  
  <pre><code>package main
  
  import (
      "fmt"
      "strconv"
  )
  
  func main() {
  
      myFloat := 899175.0
  
      x := getMaxYAxisValueForChart(myFloat)
      fmt.Println("The number to find the maximum value for is: ", myFloat)
      fmt.Println("This should be the max value for the y axis: ", x)
  }
  
  func getMaxYAxisValueForChart(float float64) (YAxisMaximum float64) {
      //Convert to string with no decimals
      floatAsString := fmt.Sprintf("%.f", float)
  
      //Get length of the string float
      floatAsStringLength := len(floatAsString)
  
      //For each digit in the string, make a zero-string
      stringPowerTen := "0"
      for i := 1; i &lt; floatAsStringLength; i++ {
          stringPowerTen += "0"
      }
  
      //Add a 1 to the 0 string to get the difference from the float
      stringPowerTenWithOne := "1" + stringPowerTen
  
      //Convert the number string to a float
      convertStringPowerTenToFloat := ConvertStringsToFloat(stringPowerTenWithOne)
  
      //Get the difference from the denominator from the numerator
      difference := convertStringPowerTenToFloat - float
  
      //We want to isolate the first digit to check how far the float is (100 is far from 1000) and then correct if so
      floatAsStringDifference := fmt.Sprintf("%.f", difference)
      runes := []rune(floatAsStringDifference)
      floatAsStringDifferenceFirstDigit := string(runes[0])
  
      //For the denominator we want to take away the difference that is rounded to the nearest ten, hundred etc
      runes = []rune(stringPowerTen)
      differenceLastDigitsAsString := ""
      if difference &lt; 10 {
          differenceLastDigitsAsString = "1"
      } else if difference &lt; 30 &amp;&amp; difference &lt; 100 {
          differenceLastDigitsAsString = "0"
      } else {
          differenceLastDigitsAsString = floatAsStringDifferenceFirstDigit + string(runes[1:])
      }
  
      //Convert the number difference string from total to a float
      convertDifferenceStringPowerTenToFloat := ConvertStringsToFloat(differenceLastDigitsAsString)
  
      YAxisMaximum = convertStringPowerTenToFloat - convertDifferenceStringPowerTenToFloat
  
      //If float is less than 10,0000
      if float &lt; 10000 &amp;&amp; (YAxisMaximum-float &gt;= 500) {
          YAxisMaximum = YAxisMaximum - 500
      }
  
      if float &lt; 10000 &amp;&amp; (YAxisMaximum-float &lt; 500) {
          YAxisMaximum = YAxisMaximum
      }
  
      //If number bigger than 10,000 then get the nearest 1,000
      if float &gt; 10000 {
  
          runes = []rune(floatAsString)
          floatAsString = string(runes[0:2])
          runes = []rune(stringPowerTen)
          stringPowerTen = string(runes[2:])
          runes = []rune(stringPowerTenWithOne)
          stringPowerTenWithOne = string(runes[0:(len(stringPowerTenWithOne) - 2)])
  
          YAxisMaximum = ConvertStringsToFloat(floatAsString+stringPowerTen) + ConvertStringsToFloat(stringPowerTenWithOne)
      }
  
      if float &gt; 10000 {
  
          runes = []rune(floatAsString)
          floatAsString = string(runes[0:2])
          runes = []rune(stringPowerTen)
          stringPowerTen = string(runes[:])
          runes = []rune(stringPowerTenWithOne)
          stringPowerTenWithOne = string(runes[0:(len(stringPowerTenWithOne))])
  
          YAxisMaximum = ConvertStringsToFloat(floatAsString+stringPowerTen) + ConvertStringsToFloat(stringPowerTenWithOne)
      }
  
      return YAxisMaximum
  }
  
  func ConvertStringsToFloat(stringToConvert string) (floatOutput float64) {
      floatOutput, Error := strconv.ParseFloat(stringToConvert, 64)
      if Error != nil {
          fmt.Println(Error)
      }
  
      return floatOutput
  }
  </code></pre>
  
  <hr>
  
  <p><strong>Here is the solution based off of Matt Timmermans answer, but converted to work in Go:</strong></p>
  
  <pre><code>func testing(float float64) (YAxisMaximum float64) {
      place := 1.0
      for float &gt;= place*10.0 {
          place *= 10.0
      }
      return math.Ceil(float/place) * place
  }
  </code></pre>