仅在 C# 中将字符串格式化为美分

发布于 2024-10-16 12:54:36 字数 260 浏览 2 评论 0原文

我有一个美元金额,23.15。我想对其进行格式化,以便只返回 0.15 或 15,因为我想将美分放在 html 标记中。

为了仅返回美元,我使用了 {0:C0} 或不使用 $ 符号 {0:N0}

编辑: 显然 { 0:C0}{0:N0} 对我不起作用,因为它们四舍五入为整数:(

I have a dollar amount, 23.15. I want to format it so that I can return just .15 or 15 as I want to place just the cents in an html <sup> tag.

To return dollars only, I used {0:C0} or without the $ sign {0:N0}

Edit: Apparently {0:C0} and {0:N0} will not work for me as they round to a whole number :(

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

想你的星星会说话 2024-10-23 12:54:36

如果您需要带有 html 标签的字符串,您可以使用如下内容:

 var decimalValue = 23.15m;
 string value2 = decimalValue.ToString("$ #.<sup>##</sup>"); //$ 23.<sup>15</sup>

另外,如果您想要带分的金额,而不是

var value =  String.Format("{0:C0}", decimalValue); // $23

在“{0:C0}”格式中的“C”后使用

 var value =  String.Format("{0:C2}", decimalValue); // $23.15

零,则表示点后的符号数。

If you need string with html tags you can use something like this:

 var decimalValue = 23.15m;
 string value2 = decimalValue.ToString("$ #.<sup>##</sup>"); //$ 23.<sup>15</sup>

Also if you want amount with cents instead of

var value =  String.Format("{0:C0}", decimalValue); // $23

use

 var value =  String.Format("{0:C2}", decimalValue); // $23.15

Zero after 'C' in '{0:C0}' format means number of signs after point.

初见你 2024-10-23 12:54:36

不是主流方式,但会起作用:)

dollarAmount - Math.Floor(dollarAmount)

会给你美分(在你的样本中会得到0.15)。

Not the mainstream way but will work :)

dollarAmount - Math.Floor(dollarAmount)

will get you cents (in your sample will get .15).

分开我的手 2024-10-23 12:54:36

班级计划
{
静态无效主(字符串[]参数)

    {

        double Num;
        // declaring "Num" as a double

        Console.WriteLine("Please enter dollar and cents amount\nExample: 4.52");
        //This bit of code informs the user what type of information you want from them

        Num = double.Parse( Console.ReadLine());
        //assigns "Num" to the users input and sets the user input as numerical value and stores it in the temporary memory

        Console.WriteLine("You have {1} dollars and {2} cents from {0:c}.", Num, WhyAreWeDoingThis(ref Num), Num);
        //returns the values requested and sends "Num" through the program to separate dollars from cents.
        //first: off in{0:c} it takes the original user input and gives it the dollar sign$ due to the additional code to the left of the zero as shown {0:c}
        //second: "Num" is sent to the WhyAreWeDoingThis Method through the reference method "ref" where the dollar amount is separated from the cent amount
        //*Note* this will only return the dollar amount not the cents*
        //Third: the second "Num" referred to at this point is only the remaining cents from the total money amount. 
        //*Note* this is because the program is moving "Num" around as a stored value from function to function not grabbing the users original input every time.

        Console.ReadLine();
        //this keeps the program open

    }
    static int WhyAreWeDoingThis(ref double A)
        // reference method
        //*Note* it is set as a "static int" and (ref double)

    {
        int dd = (int)A;
        //this turn the double into a temporary integer by type casting for this one operation only.
        A = A % dd;
        //Separates the dollars from the cents leaving only the cents in the "Num" value through the Modulus Operand.
        return dd;
        //returns the dollar amount.
    }
}

class Program
{
static void Main(string[] args)

    {

        double Num;
        // declaring "Num" as a double

        Console.WriteLine("Please enter dollar and cents amount\nExample: 4.52");
        //This bit of code informs the user what type of information you want from them

        Num = double.Parse( Console.ReadLine());
        //assigns "Num" to the users input and sets the user input as numerical value and stores it in the temporary memory

        Console.WriteLine("You have {1} dollars and {2} cents from {0:c}.", Num, WhyAreWeDoingThis(ref Num), Num);
        //returns the values requested and sends "Num" through the program to separate dollars from cents.
        //first: off in{0:c} it takes the original user input and gives it the dollar sign$ due to the additional code to the left of the zero as shown {0:c}
        //second: "Num" is sent to the WhyAreWeDoingThis Method through the reference method "ref" where the dollar amount is separated from the cent amount
        //*Note* this will only return the dollar amount not the cents*
        //Third: the second "Num" referred to at this point is only the remaining cents from the total money amount. 
        //*Note* this is because the program is moving "Num" around as a stored value from function to function not grabbing the users original input every time.

        Console.ReadLine();
        //this keeps the program open

    }
    static int WhyAreWeDoingThis(ref double A)
        // reference method
        //*Note* it is set as a "static int" and (ref double)

    {
        int dd = (int)A;
        //this turn the double into a temporary integer by type casting for this one operation only.
        A = A % dd;
        //Separates the dollars from the cents leaving only the cents in the "Num" value through the Modulus Operand.
        return dd;
        //returns the dollar amount.
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文