C#如何将数字从111.11更改为.11 [英] C# how do I change figures from 111.11 to .11

查看:102
本文介绍了C#如何将数字从111.11更改为.11的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想取一个数字说123456.7890并删除在小数点前离开.7890,所有我研究过的答案留下数字但没有小数点所以7890



我尝试了什么:



我试过这段代码,但得到的输入字符串格式不正确:在最后一行



I want to take a number say 123456.7890 and remove everthing before the decimal the leaving .7890, all the answers I have researched leave the numbers but without the decimal point so 7890

What I have tried:

I have tried this code but get a Input string was not in a correct format on exception: on the last line

int multiplier = 100;
           double double_value = Convert.ToDouble(arcflipb);
           int arcflipe = (int)((double_value - (int)double_value) * multiplier);
           textBox50.Text = "."+arcflipe.ToString();
           String archconvert = textBox50.ToString();

           decimal arcflipf = Convert.ToInt64(archconvert);

推荐答案

Use String.Split

string decPoint = ".";
string decNumber = "1241.546";
string result = decNumber.Split(CChar(decPoint))(1) //546
result = decPoint + result; //.546


您的操作没有多大意义,也没有明确描述在您打算使用它的每种情况下应该发生什么。换句话说,你的测试和边缘情况是什么?小数位数是否重要,或者它是否应该返回小数点右边的任何内容,即使那里没有任何东西(整数)?文化是否重要?不是每个人都会格式化123,456.789之类的数字。你也可能有123.456,789。



一个处理文化的快速版本可能是:

Your operation doesn't make a lot of sense, nor do you explicitly describe what should happen in every circumstance you intend to use this. In other words, what are your test and edge cases? Is the number of decimal places important, or should it return just whatever is to the right of the decimal even if there isn't anything there (integer)? Does culture matter? Not everyone formats numbers like "123,456.789". You could also have "123.456,789".

A quick'n'dirty version that handles culture could be:
/// <summary>
/// Returns a string with everything to the right of the decimal point,
/// inclusive of the decimal point, for any value type.
/// </summary>
/// <remarks>123,456.789 returns ".789"</remarks>
static string GetValueDecimalString(ValueType value)
{
    // Default return value of an Empty string.
    string returnValue = string.Empty;

    // Get the current culture and extract the decimal separator string.
    CultureInfo currentCulture = CultureInfo.CurrentCulture;
    string decimalSeparator = currentCulture.NumberFormat.NumberDecimalSeparator;

    // Convert the passed-in value to a string using the format for the current culture.
    string stringValue = Convert.ToString(value);

    // Get the index of the decimal separator in the string.
    int position = stringValue.IndexOf(decimalSeparator);

    // Return the string starting at the position the decimal separator was found.
    // If the decimal separator wasn't found, just return the default Empty string.
    if (position >= 0)
    {
        returnValue = stringValue.Substring(position);
    }

    return returnValue;
}


转换为小数进行精确计算,然后很简单。我已经创建了一个扩展方法来执行此操作:

Convert to a decimal for accurate calculations, then it is simple. I've created an extension method to do this:
static class DoubleExtension
{
    public static double GetDecimalPart(this double value)
    {
        var num = Convert.ToDecimal(value);
        return Convert.ToDouble(num - (int)num);
    }
}



使用:


To use:

double value = 12345.6789;
double result = value.GetDecimalPart();
// returns: 0.6789



现在,如果要删除小数,则需要将以下内容添加到 DoubleExtension 类:


Now, if you want to remove the decimal, you need to add the following to the DoubleExtension class:

public static int GetDecimalPart(this double value, int count)
{
    return (int)(value.GetDecimalPart() * Math.Pow(10, count));
}



然后使用:


Then to use:

double value = 12345.6789;
int result = value.GetDecimalPart(5);
// returns: 67890

int result = value.GetDecimalPart(4);
// returns: 6789

int result = value.GetDecimalPart(3);
// returns: 678





更新:戴夫正确地指出小数点,不需要前导零。但是,请求也是尾随零,所以这里有一段代码:



UPDATE: Dave rightly points out that the decimal point, no leading zero is required. However, the request is also for the trailing zero, so here is a piece of code to do that:

double value = 12345.6789;
string finalResult =


这篇关于C#如何将数字从111.11更改为.11的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆