473,385 Members | 1,402 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,385 software developers and data experts.

truncating but not rounding - string output of a decimal type value

I am writing a dietary application usng Visual C# 2010. I need to output a decimal value that can be truncated to any number of decimal places I want, without rounding at all, on any level.

I want to output the answer of a cross multiply and divide setup of 3 numeric up down control values, and output to a label without rounding at all. Each numeric up down control, is set to have 3 decimal places. Unfortunately, using math.truncate(value * 1000) / 1000 seems to output with accuracy errors (only found using more nuds). However, I do NOT want to use math.truncate any more. I want to use "pure" string output functions to accomplish that goal.

I'll also be using other nuds (numeric up down contorls) for other functions, and need perfect fractional equivalencies if testing the string output for accuracy.
May 2 '10 #1

✓ answered by Monomachus

@bodycode
Dude, try to gooogle for it. This time I'll answer, next time u'll get your LMGTFY link.

1. Use ToString methods with formating:
Example: number.ToString("F4") - will display a number with four digits after comma. Also number.ToString("#0.000") will help.

Useful links

Double.ToString Method (String) and Standard Numeric Format Strings

5 12066
Monomachus
127 Expert 100+
@bodycode
Dude, try to gooogle for it. This time I'll answer, next time u'll get your LMGTFY link.

1. Use ToString methods with formating:
Example: number.ToString("F4") - will display a number with four digits after comma. Also number.ToString("#0.000") will help.

Useful links

Double.ToString Method (String) and Standard Numeric Format Strings
May 2 '10 #2
I think your solution is flawed, if my memory serves correctly. It creates rounding.

LMGTFY is an inappropriate response since I've searched diligantly for more than a month.
May 3 '10 #3
Tested and found to be flawed. Output gets rounded.

number.ToString("#0.000") results in an answer of 76.410. However, the true value is 76.4096351. The second and third digits of the decimal are getting rounded! Why is that? I want to avoid that. I want pure truncation, no rounding. This error is entirely unacceptable.

I set up the program correctly, but, this error creeps in in the output despite all attempts at research and trying different code snippets.
May 3 '10 #4
@Monomachus
Slight rounding still occurs but it's close enough with the F4 string number/string formatting. I acquiesce to your programming opulance :)
May 3 '10 #5
I still need to merely "lop off" the resultant decimal numbers after the whole number answer, to the number of places I desire. Any error is not acceptable. The reason why I need no errors, is because the results will be used in future calculations in code, as well as some special graphing functions I'll need to incorporate into future graph results. Rounding errors, even slightly off the original precision will produce vastly innacurate results.

I'm shocked that my need is a unique one!

Anyone got a solution, please, by all means, shoot!

I've found that "F3" format string is good, but still, sometimes, there's an error creeps in (I think).

I think I can use some complex code that extracts numbers from strings, variables, or array positions that will result in a selectable number of digits.

Marshall
Jul 31 '10 #6

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

Similar topics

20
by: Raoul Watson | last post by:
By any chance, anyone got a rounding routine that does a work around the VB "round" bug? I still find it amazing that a company as large as Microsoft would put out a math package that is so...
3
by: Caesar Augustus | last post by:
Hello Tom, Long time listener, first time caller... I have been working with vb code to automate a salary increase process that not only rounds to the nearest penny but also rounds to the...
5
by: VISHNU VARDHAN REDDY UNDYALA | last post by:
Hello, Can someone over here help me in truncating a float variable. I mean if PI=3.14159 ...How can I get to read the first two or first three decimal values with out rounding them. Any...
3
by: Norvin Laudon | last post by:
Hi, Can somebody explain the following, from the MSDN documentation for the "System.Convert.ToInt32(double)" function <quote> Return Value value rounded to the nearest 32-bit signed...
4
by: spebola | last post by:
I am using vb.net 2003 professional and I get the following results when using the round method: dim Amount as decimal = 180.255 Amount = Amount.Round(Amount, 2) Amount now contains 180.25. ...
9
by: Joe Attardi | last post by:
Hi all, Math is not my strongest area so forgive me if I use some of the wrong terminology. It seems that scientific notation is immune to rounding errors. For example: (4.98 * 100) + 5.51 ...
5
by: Jason | last post by:
I am having a rounding problem in a value i am trying to display in VB.NET. If I have the following code: Dim x As Single = 2726.795 Dim y As Single = Math.Round(2726.795, 2) Dim s as String =...
29
by: Marco | last post by:
Hello, I have : float f = 36.09999999; When I do : char cf; sprintf(cf,"%0.03lf", f); I get : 36.100
7
by: Tony Girgenti | last post by:
Hello. Trying to develop VS2005, SP1 windows form VB program. When i try a statement like below and the data is "24.95", without the quotes of course, regPriceString equals "000000000025+" . ...
206
by: md | last post by:
Hi Does any body know, how to round a double value with a specific number of digits after the decimal points? A function like this: RoundMyDouble (double &value, short numberOfPrecisions) ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.