duanqiongchong0354 2018-12-11 19:27
浏览 59
已采纳

如何改善我的功能,如果2位数字四舍五入到最接近的10,如果3位数字等则到100

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

My approach is not ideal but works like this:

  • Get a number to round, like 829
  • Count the number of digits (3)
  • Use a loop to convert to a string of 0s ("000")
  • Add a 1 to the start of the string then convert to a float (1000)
  • Find the difference (1000 - 829 = 171)
  • 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)

This means that 725 will see a y axis max label number of 800, and 829 of 900

My code works, but I feel like it's a piece of crap with a hacky approach

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

How can I improve here? I'm a little stuck, is my idea of converting to strings even right?!

Full code here:

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 < 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 < 10 {
        differenceLastDigitsAsString = "1"
    } else if difference < 30 && difference < 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 < 10000 && (YAxisMaximum-float >= 500) {
        YAxisMaximum = YAxisMaximum - 500
    }

    if float < 10000 && (YAxisMaximum-float < 500) {
        YAxisMaximum = YAxisMaximum
    }

    //If number bigger than 10,000 then get the nearest 1,000
    if float > 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 > 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
}

Here is the solution based off of Matt Timmermans answer, but converted to work in Go:

func testing(float float64) (YAxisMaximum float64) {
    place := 1.0
    for float >= place*10.0 {
        place *= 10.0
    }
    return math.Ceil(float/place) * place
}
  • 写回答

4条回答 默认 最新

  • doudeng3008 2018-12-11 19:48
    关注

    Wow, that's a pretty complicated procedure you have. This is how I would do it if the numbers aren't enormous. I don't know go, so I'm going to guess about how to write it in that language:

    func getMaxYAxisValueForChart(float float64) {
    
        place := 1.0;
        while float >= place*10.0 {
            place *= 10.0;
        }
        return math.Ceil(float/place) * place;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用