473,406 Members | 2,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Formating String Number Decimal places

Hi
I am having trouble trying to format a number string to two decimal places
if the number ends in a 0
It formats fine otherwise.

For example if PrincRepay is 78.20 it will display it as 78.2
PrincRepay, payment and IntAmt are double

PrincRepay = Math.Round(payment - IntAmt,2);
PrincRepayStr = PrincRepay.ToString("N2");

Is there anyway around this

Thanks in advance
Bill
Nov 17 '05 #1
6 16621
Hi Bill,

Try this:

System.Globalization.NumberFormatInfo nfi = new
System.Globalization.NumberFormatInfo();
nfi.NumberDecimalDigits = 2;

double i = 78.2;
string s = i.ToString("N", nfi);
Console.WriteLine(s); // Will output 78.20

Regards,

Pete

"Bill" wrote:
Hi
I am having trouble trying to format a number string to two decimal places
if the number ends in a 0
It formats fine otherwise.

For example if PrincRepay is 78.20 it will display it as 78.2
PrincRepay, payment and IntAmt are double

PrincRepay = Math.Round(payment - IntAmt,2);
PrincRepayStr = PrincRepay.ToString("N2");

Is there anyway around this

Thanks in advance
Bill

Nov 17 '05 #2
Hi Pete

I tried this however I received the same result

System.Globalization.NumberFormatInfo nfi = new

System.Globalization.NumberFormatInfo();

nfi.NumberDecimalDigits = 2;

PrincRepayStr = PrincRepay.ToString("N", nfi);

Bill

"Peter" <Pe***@discussions.microsoft.com> wrote in message
news:43**********************************@microsof t.com...
Hi Bill,

Try this:

System.Globalization.NumberFormatInfo nfi = new
System.Globalization.NumberFormatInfo();
nfi.NumberDecimalDigits = 2;

double i = 78.2;
string s = i.ToString("N", nfi);
Console.WriteLine(s); // Will output 78.20

Regards,

Pete

"Bill" wrote:
Hi
I am having trouble trying to format a number string to two decimal
places
if the number ends in a 0
It formats fine otherwise.

For example if PrincRepay is 78.20 it will display it as 78.2
PrincRepay, payment and IntAmt are double

PrincRepay = Math.Round(payment - IntAmt,2);
PrincRepayStr = PrincRepay.ToString("N2");

Is there anyway around this

Thanks in advance
Bill

Nov 17 '05 #3
Hi Bill,

Perhaps you are running a different CurrentCulture that I am. Try the code
below which specifies the culture. If this does not work, maybe the problem
is somewhere else along the line.

System.Globalization.NumberFormatInfo nfi = new
System.Globalization.CultureInfo("en-US", false).NumberFormat;
nfi.NumberDecimalDigits = 2;

double PrincRepay = 78.2;
string PrincRepayStr = PrincRepay.ToString("N", nfi);

// PrincRepayStr is "78.20"

Regards,

Peter

"Bill" wrote:
Hi Pete

I tried this however I received the same result

System.Globalization.NumberFormatInfo nfi = new

System.Globalization.NumberFormatInfo();

nfi.NumberDecimalDigits = 2;

PrincRepayStr = PrincRepay.ToString("N", nfi);

Bill

"Peter" <Pe***@discussions.microsoft.com> wrote in message
news:43**********************************@microsof t.com...
Hi Bill,

Try this:

System.Globalization.NumberFormatInfo nfi = new
System.Globalization.NumberFormatInfo();
nfi.NumberDecimalDigits = 2;

double i = 78.2;
string s = i.ToString("N", nfi);
Console.WriteLine(s); // Will output 78.20

Regards,

Pete

"Bill" wrote:
Hi
I am having trouble trying to format a number string to two decimal
places
if the number ends in a 0
It formats fine otherwise.

For example if PrincRepay is 78.20 it will display it as 78.2
PrincRepay, payment and IntAmt are double

PrincRepay = Math.Round(payment - IntAmt,2);
PrincRepayStr = PrincRepay.ToString("N2");

Is there anyway around this

Thanks in advance
Bill


Nov 17 '05 #4
Hi Bill,
I am not sure why I get this and you don't but if I do the following:

double d = 78.2;
Console.WriteLine(d.ToString("N2"));

Then I get 78.20 printed on the Console.

"Bill" wrote:
Hi
I am having trouble trying to format a number string to two decimal places
if the number ends in a 0
It formats fine otherwise.

For example if PrincRepay is 78.20 it will display it as 78.2
PrincRepay, payment and IntAmt are double

PrincRepay = Math.Round(payment - IntAmt,2);
PrincRepayStr = PrincRepay.ToString("N2");

Is there anyway around this

Thanks in advance
Bill

Nov 17 '05 #5
PW

"Mark R. Dawson" <Ma*********@discussions.microsoft.com> wrote in message
news:E5**********************************@microsof t.com...
Hi Bill,
I am not sure why I get this and you don't but if I do the following:

double d = 78.2;
Console.WriteLine(d.ToString("N2"));

Then I get 78.20 printed on the Console.


This works for me as well. You might try:

d.ToString("0.00")

Although I suspect there is something else fouling up your code.

Nov 17 '05 #6
Thanks that worked!

Hi Bill,
I am not sure why I get this and you don't but if I do the following:

double d = 78.2;
Console.WriteLine(d.ToString("N2"));

Then I get 78.20 printed on the Console.


This works for me as well. You might try:

d.ToString("0.00")

Although I suspect there is something else fouling up your code.

Nov 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Brent Bortnick | last post by:
Does anyone know how to find out the number of decimal places a number has. I need this info so that i can round to 3 decimal places if the number has 3 decimal places or to 2 decimal places if...
1
by: MAF | last post by:
How can I set the number of decimal places for a double value?
3
by: Mark | last post by:
I have a Decimal or a Double - your choice. I'd like to print the number as string with a specified number of decimal places. Let's say my number 110.5, and I'd like to capture it in a string...
4
by: Phil Mc | last post by:
OK this should be bread and butter, easy to do, but I seem to be going around in circles and not getting any answer to achieving this simple task. I have numbers in string format (they are...
6
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?...
19
by: VK | last post by:
http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/ b495b4898808fde0> is more than one month old - this may pose problem for posting over some news servers. This is why I'm...
17
by: scan87 | last post by:
Can somone please, please give me the solution for the following problem. I need to submit it on Monday. Write a global function called format, which formats numbers with a given number of decimal...
1
by: Hiranmoy | last post by:
I have table with some fields in which a field contains values like 42.32, 64.68, 80.30, 70.95 etc. Is it possible to design the number format be always Rounded Up like 43, 65, 81, 71 etc.? I have...
4
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I format a Number as a String with exactly 2 decimal places?...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.