473,396 Members | 2,036 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,396 software developers and data experts.

how to throw the characters?

3
I have a number of type double 12.2301000
I need to get 12.23

p.s. I do not need rounding.
Mar 10 '12 #1
3 1608
PsychoCoder
465 Expert Mod 256MB
You can use String.Format like this to only allow 2 decimal places:

Expand|Select|Wrap|Line Numbers
  1. String.Format("{0:0.00}", 123.4567);
You can also use regular expressions in the KeyPress event of your textbox like this:

Expand|Select|Wrap|Line Numbers
  1. private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
  2. {
  3.     if (char.IsNumber(e.KeyChar) || e.KeyChar=='.')
  4.     {
  5.         if (Regex.IsMatch(textBox1.Text, "^\\d*\\.\\d{2}$")) 
  6.             e.Handled=true;
  7.     }
  8.     else 
  9.     e.Handled = e.KeyChar != (char)Keys.Back;
  10. }
Mar 10 '12 #2
iurceg
3
PsychoCoder
Thanks for response but the first version is rounded.
Expand|Select|Wrap|Line Numbers
  1. String.Format("{0:0.00}", 123.4567);
The result is: 123.46 but i need 123.45 :)
Mar 10 '12 #3
Stewart Ross
2,545 Expert Mod 2GB
@iurceg: what you are really asking is that the result should be rounded down in all cases, not rounded up when the next digit is 5 or more as is the normal practice. It is not correct to say you do not need rounding - truncation is still a form of rounding of the answer.

I'm not a c# person, so I don't know how to express it in c#, but one way to truncate a value to two decimal places is to multiply the double by 100, store the result in a longint, then divide the longint value by 100 to obtain your result (effectively truncating to two decimal places max).

You could also use integer division in place of the use of a longint, or whatever the C# equivalent of the VB Int function is to accomplish the same effect.

-Stewart
Mar 10 '12 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Curt Fluegel | last post by:
I seem to be having a problem base64 encoding characters above 127. I can encode a sentence like "The big bad dog" without problems, but if I try to encode something like 0xFF I get different...
2
by: Ross Noe via .NET 247 | last post by:
(Type your message here) -------------------------------- From: Ross Noe I created an XML file using ASP that has Russian characters. Forsome reason ASP.Net doesn't read the Russian...
25
by: Wim Cossement | last post by:
Hello, I was wondering if there are a few good pages and/or examples on how to process form data correctly for putting it in a MySQL DB. Since I'm not used to using PHP a lot, I already found...
6
by: Henryk | last post by:
I did a lot of delphi GUI programming recently. In my experience most of the time you just want to throw a standard exception with a descriptive message. Then all calling functions can handle...
4
by: Vlad | last post by:
I am having problems using the file.create method within a function that is called when looping through an array of filepaths. If I call my function with a hardcoded file path --C:\Temp.txt the...
10
by: silverburgh.meryl | last post by:
Hi, Is there a string function to trim all non-ascii characters out of a string? Let say I have a string in python (which is utf8 encoded), is there a python function which I can convert that...
1
by: aemado | last post by:
I am trying to read in several lines, each should have exactly 5 pieces of data. I am using try/catch/throw to determine if the data is in the correct format, and trying to use iss to separate the...
6
by: cmdolcet69 | last post by:
I have a value bieng added into my array that displays correctly in the textbox as 0.10 however when i read from my array into an excel spreadsheet I get the value 0.10 how can I get rid of the ...
13
by: Liang Chen | last post by:
Hope you all had a nice weekend. I have a question that I hope someone can help me out. I want to run a Python program that uses Tkinter for the user interface (GUI). The program allows me to type...
5
by: raagadeepthi | last post by:
I need to throw an alert saying "Enetered special characters " alert when i click on submit button in javascript. Need code for this Please help!!!!!
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
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,...

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.