c# - Format a double to two digits after the comma without rounding up or down -


i have been searching forever , cannot find answer, none of them work properly.

i want turn double 0.33333333333 0,33 or 0.6666666666 0,66

number 0.9999999999 should become 1 though.

i tried various methods like

value.tostring("##.##", system.globalization.cultureinfo.invariantculture) 

it returns garbage or rounds number wrongly. please?

basically every number divided 9, needs displayed 2 decimal places without rounding.

i have found nice function seems work numbers 9.999999999 beyond starts lose 1 decimal number. number 200.33333333333 going display 200 instead of 200,33. fix guys?

here is:

string truncate(double value, int precision) {     string result = value.tostring();      int dot = result.indexof(',');     if (dot < 0)     {         return result;     }      int newlength = dot + precision + 1;      if (newlength == dot + 1)     {         newlength--;     }      if (newlength > result.length)     {         newlength = result.length;     }      return result.substring(0, newlength); } 

have tried

math.round(0.33333333333, 2); 

update*

if don't want decimal rounded thing can change double string , get substring 2 decimal places , convert double.

doublestring = double.tostring(); if(doublestring.indexof(',') > -1) {    doublestring = doublestring.substring(0,doublestring.indexof(',')+3); } double = convert.todouble(doublestring); 

you can use if statement check .99 , change 1 case.


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -