Why does Format.String do rounding?

  • Thread starter Thread starter Tony Girgenti
  • Start date Start date
T

Tony Girgenti

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
 
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.
 
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
 
Tony Girgenti wrote:
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.
 
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
 
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
 
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
 
Tony said:
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)
 
Back
Top