473,473 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Convert String to float and still retain the precision

3 New Member
I am trying to write a method that have this signature in C#
string ConvertFloatToString(float floatNumber, int precision)
if we call ConvertFloatToString (1234.3433,2) then the method should return "1234.34"

Can give me some idea?
Oct 29 '08 #1
5 1726
tlhintoq
3,525 Recognized Expert Specialist
I am trying to write a method that have this signature in C#
string ConvertFloatToString(float floatNumber, int precision)
if we call ConvertFloatToString (1234.3433,2) then the method should return "1234.34"

Can give me some idea?
This is built into a float type. Use the .ToString() method
Expand|Select|Wrap|Line Numbers
  1.             float bob = 123.456f;
  2.             string szBob = bob.ToString("0.00");
  3.             MessageBox.Show(szBob);
  4.  
Other format samples
iFormatProvider example
Oct 29 '08 #2
tumblypooh
3 New Member
This is built into a float type. Use the .ToString() method
Expand|Select|Wrap|Line Numbers
  1.             float bob = 123.456f;
  2.             string szBob = bob.ToString("0.00");
  3.             MessageBox.Show(szBob);
  4.  
Other format samples
iFormatProvider example
We can use the Convert.ToString() which is the built-in function, we need to invet our own algorithm to implement the method. :(
any thought?
Oct 29 '08 #3
Plater
7,872 Recognized Expert Expert
This must be a home work assignment type thing then?
Please read the posting guidelines, which covers why we do not answer homework questions
Oct 29 '08 #4
tumblypooh
3 New Member
This must be a home work assignment type thing then?
Please read the posting guidelines, which covers why we do not answer homework questions
This is not a homework assignment as I am not studying but I am a kind of person who is interested in doing programming challenging problem in terms of efficiency.
Oct 29 '08 #5
Plater
7,872 Recognized Expert Expert
You are not going to get more effecient then the .ToString() method without using unmanaged code.
Oct 29 '08 #6

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

Similar topics

4
by: jaijai_kumar | last post by:
Select Cast('100.1234' as float) give me the result 100.1234 Now when I convert it back to char I want exactly 100.1234 Select Convert(char(100),Cast('100.1234' as float)) Gives me 100.123 (Here...
2
by: Goran | last post by:
Hi! I need to convert from a unsigned char array to a float. I don't think i get the right results in the program below. unsigned char array1 = { 0xde, 0xc2, 0x44, 0x23}; //I'm not sure in...
8
by: vijay | last post by:
Hello, What happens to float variable in loops. For example, float f=8.7; if(f<8.7) printf("less"); else if(f==8.7) printf("equal"); else if(f>8.7)
7
by: A. L. | last post by:
Consider following code segment: #1: double pi = 3.141592653589; #2: printf("%lf\n", pi); #3: printf("%1.12lf\n", pi); #4: printf("%1.15lf\n", pi); The above code outputs as following: ...
0
by: Thaddeus | last post by:
//Author: //Thaddeus Jacobs, MCP //Kinematic Automation, Inc. //mailto:tjacobs@kinematic.com // //Description: //convert ADO .NET dataset to ADO 2.5 2.6 2.7 recordset and v/v //DataSet to...
3
by: David Lozzi | last post by:
Howdy, ISSUE 1: See issue 2 below. I have a distance calculator on my site which works great. However, the users need to sort by distance, which make sense. I'm not sure how to do it other than...
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...
60
by: Erick-> | last post by:
hi all... I've readed some lines about the difference between float and double data types... but, in the real world, which is the best? when should we use float or double?? thanks Erick
2
by: ireyes | last post by:
DEAR SIR, I SAW YOUR INTERNET QUESTION AND I HAVE THE SAME TROUBLE. CUOLD YOU HELP ME TO MAKE A FLOAT TO INTEGER CONVERTION? DO YOU HAVE ANY EXEL FILE THAT CAN DO THAT? REGARDS AND THANKS A LOT ...
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,...
1
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...
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,...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
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.