473,406 Members | 2,387 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.

Why does Format.String do rounding?

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+" .

regPriceString = String.Format("{0:000000000000+}",
merchandiseDataRow.Item("RegPrice"))

Why does it round the number? What does formatting have to do with math?

I would like regPriceString to wind up with "000002495000+" I don't want
the decimal point in the result.

Any help would be gratefully appreciated.

Thanks,
Tony
Mar 17 '07 #1
7 5451
"Tony Girgenti" <tony(nospam)@lakesideos.comwrote in news:e70
$j***********@TK2MSFTNGP05.phx.gbl:
regPriceString = String.Format("{0:000000000000+}",
merchandiseDataRow.Item("RegPrice"))

Why does it round the number? What does formatting have to do with math?
How about try

0.00

I think that'll round to 2 decimal places.
Mar 17 '07 #2
Hello Spam.

I guess i didn't explain my problem correctly.

Your suggestion gives the result of "25.00+". I would like the result to be
without a decimal point. If my source data is "24.95", i want it to be
formatted as "000002495000+" before i write it to a file.

I hope that explains what i want to do.

Thanks,
Tony

"Spam Catcher" <sp**********@rogers.comwrote in message
news:Xn**********************************@127.0.0. 1...
"Tony Girgenti" <tony(nospam)@lakesideos.comwrote in news:e70
$j***********@TK2MSFTNGP05.phx.gbl:
>regPriceString = String.Format("{0:000000000000+}",
merchandiseDataRow.Item("RegPrice"))

Why does it round the number? What does formatting have to do with math?

How about try

0.00

I think that'll round to 2 decimal places.

Mar 17 '07 #3
Tony Girgenti wrote:
<snip>
When i try a statement like below and the data is "24.95", without the
quotes of course, regPriceString equals "000000000025+" .

regPriceString = String.Format("{0:000000000000+}",
merchandiseDataRow.Item("RegPrice"))

Why does it round the number? What does formatting have to do with math?

I would like regPriceString to wind up with "000002495000+" I don't want
the decimal point in the result.
<snip>

First of all, I don't see how the code that converts 24.95 to string
(probably Double.ToString) would guess that you wanted its value
multiplied by 100000 prior to conversion. You must do that math
yourself, I suppose. ;-)

regPriceString = String.Format("{0:000000000000+}", _
CType(merchandiseDataRow.Item("RegPrice"), Double) * 100000)

Now, for the "000000000025+" output, it seems to be the result of the
conversion applying rounding to accomodate for the last significant
digit in the format mask you provided. There would be rounding also if
the mask had place for just one decimal digit.

HTH.

Regards,

Branco.

Mar 17 '07 #4
Hello Branco.

Thanks for your reply.

I'm not sure of what your point is in your first observation.

Your second observation might be true, i don't know, but i need an output as
"000002495000+" from an input of "24.95".

Thanks,
Tony

"Branco Medeiros" <br*************@gmail.comwrote in message
news:11**********************@d57g2000hsg.googlegr oups.com...
Tony Girgenti wrote:
<snip>
>When i try a statement like below and the data is "24.95", without the
quotes of course, regPriceString equals "000000000025+" .

regPriceString = String.Format("{0:000000000000+}",
merchandiseDataRow.Item("RegPrice"))

Why does it round the number? What does formatting have to do with math?

I would like regPriceString to wind up with "000002495000+" I don't want
the decimal point in the result.
<snip>

First of all, I don't see how the code that converts 24.95 to string
(probably Double.ToString) would guess that you wanted its value
multiplied by 100000 prior to conversion. You must do that math
yourself, I suppose. ;-)

regPriceString = String.Format("{0:000000000000+}", _
CType(merchandiseDataRow.Item("RegPrice"), Double) * 100000)

Now, for the "000000000025+" output, it seems to be the result of the
conversion applying rounding to accomodate for the last significant
digit in the format mask you provided. There would be rounding also if
the mask had place for just one decimal digit.

HTH.

Regards,

Branco.

Mar 17 '07 #5
Looking around I don't quite see the format string you're using but perhaps
you can adapt this example. My "guess" is that you're seeing rounding
because there is no decimal point in your format string. You just don't
want it to appear but the formatter interprets that as "round the number
because he doesn't want the fraction". I've had it print out a string
literal and the formatted version so you can see they are identical.

Note that you must multiply your value by 100 to eliminate the fractional
part.

Dim regPrice As Int64 = CLng(24.95 * 100)

Debug.WriteLine("000002495000+")
Debug.WriteLine(regPrice.ToString("000000000\0\0\0 +"))

And of course you can assign it to a string var if you prefer:

Dim regPriceString As String = regPrice.ToString("000000000\0\0\0+")
Debug.WriteLine(regPriceString)

Or even eliminate the interim regPrice variable:

Dim regPriceString As String = CLng(24.95 *
100).ToString("000000000\0\0\0+")
Debug.WriteLine(regPriceString)

Does this work for you?
Tom

"Tony Girgenti" <tony(nospam)@lakesideos.comwrote..
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+" .

regPriceString = String.Format("{0:000000000000+}",
merchandiseDataRow.Item("RegPrice"))

Why does it round the number? What does formatting have to do with math?

I would like regPriceString to wind up with "000002495000+" I don't want
the decimal point in the result.

Any help would be gratefully appreciated.

Thanks,
Tony


Mar 17 '07 #6
Hello Tom.

You obviously put almost as much effort into this problem as i have. Thanks
for that.

I did a workaround and it works, but i will try your method and let you know
how it works out.

Thanks,
Tony

"Tom Leylan" <tl*****@nospam.netwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
Looking around I don't quite see the format string you're using but
perhaps you can adapt this example. My "guess" is that you're seeing
rounding because there is no decimal point in your format string. You
just don't want it to appear but the formatter interprets that as "round
the number because he doesn't want the fraction". I've had it print out a
string literal and the formatted version so you can see they are
identical.

Note that you must multiply your value by 100 to eliminate the fractional
part.

Dim regPrice As Int64 = CLng(24.95 * 100)

Debug.WriteLine("000002495000+")
Debug.WriteLine(regPrice.ToString("000000000\0\0\0 +"))

And of course you can assign it to a string var if you prefer:

Dim regPriceString As String = regPrice.ToString("000000000\0\0\0+")
Debug.WriteLine(regPriceString)

Or even eliminate the interim regPrice variable:

Dim regPriceString As String = CLng(24.95 *
100).ToString("000000000\0\0\0+")
Debug.WriteLine(regPriceString)

Does this work for you?
Tom

"Tony Girgenti" <tony(nospam)@lakesideos.comwrote..
>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+" .

regPriceString = String.Format("{0:000000000000+}",
merchandiseDataRow.Item("RegPrice"))

Why does it round the number? What does formatting have to do with math?

I would like regPriceString to wind up with "000002495000+" I don't want
the decimal point in the result.

Any help would be gratefully appreciated.

Thanks,
Tony



Mar 18 '07 #7
Tony Girgenti wrote:
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+" .

regPriceString = String.Format("{0:000000000000+}",
merchandiseDataRow.Item("RegPrice"))

Why does it round the number? What does formatting have to do with math?
A floating point value is always rounded when formatted to a specific
format. It has to be rounded to the number of decimals that you
specified in the format, regardless if it is five decimals or zero.
I would like regPriceString to wind up with "000002495000+" I don't want
the decimal point in the result.
I first thought that creating a NumberFormatInfo object with an empty
string as decimal separator would easily enable you to format a value
with an invisible decimal point, but the decimal separator can not be
assigned an empty string.

You just have to adjust the value before formatting it:

regPriceString = String.Format("{0:000000000000+}",
merchandiseDataRow.Item("RegPrice") * 100000)

--
Göran Andersson
_____
http://www.guffa.com
Mar 18 '07 #8

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

Similar topics

3
by: stevek | last post by:
How do I format an integer. Add commas. 1234565 1,234,565 TIA
1
by: Mike MacSween | last post by:
This looks like a bug to me. I have an expression on a report: =Format(Sum((**)*(/)),"0.00") Score is byte PercentOfGrade is double PropDegree is single ModuleCats is byte
3
by: Jean | last post by:
Hi, I have a query that contains the following in the WHERE part of the query: WHERE....Format(IIf(nz()<>0,, IIf(nz()<>0,, IIf(nz()<>0,, ))),"0.0") AS Körperhöhe,....
3
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 =...
6
by: Donal McWeeney | last post by:
Hi, Is there a way to specify on the predefined format strings like P and N that you want displayed all the decimals in the number... for example 3 will display 3 2.1 will display 2.1...
29
by: Vol | last post by:
I think 'atan' can get the angle but it is not the four quadrant angle. Is there any function that i can get the angle from -pi to pi? or I have to use some if ... else? I know in Matlab, we use...
52
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?...
130
by: Daniel Manes | last post by:
I'm baffled. I have a column in a SQL Server Express database called "Longitude," which is a float. When I view the table in a DataGridView, some of the numbers, which only have two 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...
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
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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.