Get the App
SLTechnology News&Howtos  ›  Development  › 

How to keep the specified number of decimal places in A. net decimal

Shulou Source: shulou.com Published: 2022-06-03 13:09:35 10月03日 Update

How to keep the specified number of decimal places in A. net decimal, aiming at this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

In the case of the assessed amount encountered in the project, the amount of the last article = the total amount-the sum of the assessed amount.

This may cause the last allocation to be negative, so I wrote a way to keep the specified number of decimal places.

The use of extension methods makes the invocation elegant.

Sample code

Public static class DecimalExtension {/ decimal retains the specified number of decimal places / original number / keeps the number of decimal places / intercepts the number string public static string ToString (this decimal num, int scale) {string numToString = num.ToString (); int index = numToString.IndexOf ("."); int length = numToString.Length If (index! =-1) {return string.Format ("{0}. {1}", numToString.Substring (0, index), numToString.Substring (index + 1, Math.Min (length-index-1, scale));} else {return num.ToString () This is the answer to the question about how to keep the specified decimal places in .net decimal. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

Tags: Digits decimal amount method question quantity more help answer original easy elegant easy code content character string boy buddy situation Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Shulou Information macOS Xiaomi Shulou Tech Info