473,511 Members | 15,046 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

format for double type

Hi all,

just wanted to know how can I just quickly round to nearest
100th, and use the first two decimal places of a double value in a textbox,
instead of all the decimals.

Thanks in advance,

Carlos


Nov 17 '05 #1
2 2865
When it comes to rounding, take a look at Math.Round(). In order to round a
var to the 100th’s place, you’d simply do something like:

double pi = 3.14159265;
pi = Math.Round(pi, 2);

In this example, pi ends up with the value 3.14 because of the second
argument passed into Round() specifies the # of decimal places you want to
round to. After this, you simply output the variable to where you see fit.

Brendan
"Carlos" wrote:
Hi all,

just wanted to know how can I just quickly round to nearest
100th, and use the first two decimal places of a double value in a textbox,
instead of all the decimals.

Thanks in advance,

Carlos


Nov 17 '05 #2
Hi,

Carlos wrote:
just wanted to know how can I just quickly round to nearest
100th, and use the first two decimal places of a double value in a
textbox, instead of all the decimals.


Brendan showed you how to round, if you need to. However, if all you are
doing is populating a text box, you will be converting the double to a
string anyway. You could just let the framework do it while formatting the
double as string.

double d = 12.3456789;
Console.WriteLine(d.ToString("N"));

This produces the following output:

12.35

As you can see, the number was correctly rounded during formatting and if
you are not doing anything else with it, then prior rounding in your code
isn't necesary. If you don't want thousands separated, you can use the
custom format string "0.00" instead of standard "N".
--
Chris Priede
Nov 17 '05 #3

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

Similar topics

11
3677
by: John Lenton | last post by:
Is there any reason python's printf-style formatting is missing the (C99) '%a' specifier? I'm sorry if this has been asked and answered before; I can't find it on google ('%a' is a very awkward...
6
30148
by: J | last post by:
Would anyone know if there a type tag to format a double? I have f for floating point, but cannot find one for double.
1
8803
by: Nick | last post by:
Well, the project I am working on has now come to a screeching halt! I have been developing a program that heavily utilizes ADO.NET record sets. To generate reports, I convert the recordset to XML,...
3
12419
by: Boz | last post by:
Hi, I am trying to use string.Format() to output the value of a double. double da = 100000000000.99994; double db = 100000000000.9994; double dc = 100000000000.994; double dd =...
11
2990
by: Nathan Wiegman | last post by:
Hi, Anyone know why "s" equals "0.0870000000000000000000" after the following lines execute? Thanks, Nate double d = 0.087000000000000008; string s = string.Format("{0:f22}", d);
1
1864
by: George Yachán | last post by:
I have added a new column to a typed dataset which displays an expression which is the result of one double (data type) field divided by another double field. I would like format this column to...
4
1440
by: RD | last post by:
The statements The value passed to Amount by the function call is 111111111111111.98 and is a double then we 4execute the following code snippet Dim New_amt as string New_amt =...
8
4182
by: Jayesh | last post by:
Hi, How is a double value stored in 8 bytes ? Or what is the format ? How is it different from __int64 ? How is __int64 stored in 8 bytes. Please do reply. regards, Jayesh
11
10792
by: zefciu | last post by:
I am trying to embed a c function in my python script for a first time. When I try to call it I get an error SystemError: new style getargs format but argument is not a tuple Guido said on...
2
3603
by: JK | last post by:
The following is a sample program I wrote.. float fTick = 0.0001; float fLotSize = 100; int iDeno = 10000; double dVal; double dInt; dInt = fTick * fLotSize;
0
7237
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
7137
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
7506
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...
0
5659
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5063
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4734
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3219
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1572
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
445
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.