DecimalFormat khung String pattern = "";for (int i = 0; i EDIT The particular problem is that I do not wish the decimal separator lớn change with Locale (e.g. some Locales would use ",").

Bạn đang xem: Làm cách nào để định dạng một số trong java?


For decimal separator you can mix it in a NumberFormatInfo instance và use it with ToString:

NumberFormatInfo nfi = new NumberFormatInfo(); nfi.NumberDecimalSeparator = "."; //** chạy thử ** NumberFormatInfo nfi = new NumberFormatInfo(); decimal d = 125501.0235648m; nfi.NumberDecimalSeparator = "?"; s = d.ToString(nfi); //--> 125501?0235648khổng lồ have sầu the result of your java version, use the ToString() function version with Custom Numeric Format Strings (i.e.: what you called pattern):

s = d.ToString("# ### ##0.0000", nfi);// 1245124587.23 --> 1245 124 587?2300 // 24587.235215 --> 24 587?2352System.Globalization.NumberFormatInfo


In C#, decimal numbers are stored in the decimal type, with an internal representation that allows you lớn perkhung decimal math without rounding errors.

Once you have sầu the number, you can format it using Decimal.ToString() for output purposes. This formatting is locale-specific; it respects your current culture setting.

Xem thêm: Cấu Trúc Allow, Cách Sử Dụng Advised, Cách Sử Dụng Advised


Thanks for contributing an answer to lớn Stachồng Overflow!

Please be sure to answer the question. Provide details & nói qua your research!

But avoid

Asking for help, clarification, or responding to other answers.Making statements based on opinion; bachồng them up with references or personal experience.

To learn more, see our tips on writing great answers.

Post Your Answer Disthẻ

By clicking “Post Your Answer”, you agree khổng lồ our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged c# java decimalformat or ask your own question.


site kiến thiết / hình ảnh sản phẩm © 2021 Staông chồng Exchange Inc; user contributions licensed under cc by-sa. rev2021.6.17.39529

Stack Overflow works best with JavaScript enabled

Your privacy

By clicking “Accept all cookies”, you agree Staông xã Exchange can store cookies on your device and discthảm bại information in accordance with our Cookie Policy.