truncate

How can I truncate a double to only two decimal places in Java? [duplicate]

删除回忆录丶 提交于 2019-11-26 17:35:26
This question already has an answer here: How to round a number to n decimal places in Java 29 answers For example I have the variable 3.545555555, which I would want to truncate to just 3.54. Bozho If you want that for display purposes, use java.text.DecimalFormat : new DecimalFormat("#.##").format(dblVar); If you need it for calculations, use java.lang.Math : Math.floor(value * 100) / 100; Cedric Dubourg DecimalFormat df = new DecimalFormat(fmt); df.setRoundingMode(RoundingMode.DOWN); s = df.format(d); Check available RoundingMode and DecimalFormat . Mani Bit Old Forum, None of the above

Truncate NSDate (Zero-out time)

谁都会走 提交于 2019-11-26 15:48:38
问题 I want to generate a new NSDate with 0 hours , 0 minutes , and 0 seconds for time. The source date can be any random NSDate . Is there a way to achieve this? The documentation did not help me with this. Example Have: 2010-10-30 10:14:13 GMT Want: 2010-10-30 00:00:00 GMT 回答1: unsigned int flags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay; NSCalendar* calendar = [NSCalendar currentCalendar]; NSDateComponents* components = [calendar components:flags fromDate:date]; NSDate*

I want to truncate a text or line with ellipsis using JavaScript [closed]

五迷三道 提交于 2019-11-26 15:45:01
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 2 years ago . I'm looking for a simple script which can truncate a string with ellipsis (...) I want to truncate something like 'this is a very long string' to 'this is a ve...' I don't want to use CSS or PHP. 回答1: function truncate(input) { if (input.length > 5) return input.substring(0,5) + '...'; else return input; }; or

How to truncate a file in c#?

不羁岁月 提交于 2019-11-26 14:44:39
问题 I am writing actions done by the program in C# into a file by using Trace.Writeln() function. But the file is becoming too large. How to truncate this file when it grows to 1MB? TextWriterTraceListener traceListener = new TextWriterTraceListener(File.AppendText("audit.txt")); Trace.Listeners.Add(traceListener); Trace.AutoFlush = true; What should be added to the above block 回答1: Try to play around with FileStream.SetLength FileStream fileStream = new FileStream(...); fileStream.SetLength

When an int is cast to a short and truncated, how is the new value determined?

倖福魔咒の 提交于 2019-11-26 14:18:47
问题 Can someone clarify what happens when an integer is cast to a short in C? I'm using Raspberry Pi, so I'm aware that an int is 32 bits, and therefore a short must be 16 bits. Let's say I use the following C code for example: int x = 0x1248642; short sx = (short)x; int y = sx; I get that x would be truncated, but can someone explain how exactly? Are shifts used? How exactly is a number truncated from 32 bits to 16 bits? 回答1: According to the ISO C standard, when you convert an integer to a

What is a unix command for deleting the first N characters of a line?

我的未来我决定 提交于 2019-11-26 12:49:44
问题 For example, I might want to: tail -f logfile | grep org.springframework | <command to remove first N characters> I was thinking that tr might have the ability to do this but I\'m not sure. 回答1: Use cut . Eg. to strip the first 4 characters of each line (i.e. start on the 5th char): tail -f logfile | grep org.springframework | cut -c 5- 回答2: sed 's/^.\{5\}//' logfile and you replace 5 by the number you want...it should do the trick... EDIT if for each line sed 's/^.\{5\}//g' logfile 回答3: You

Cannot truncate table because it is being referenced by a FOREIGN KEY constraint?

徘徊边缘 提交于 2019-11-26 12:34:51
Using MSSQL2005, can I truncate a table with a foreign key constraint if I first truncate the child table (the table with the primary key of the FK relationship)? I know that I can either Use a DELETE without a where clause and then RESEED the identity (or) Remove the FK, truncate the table, and recreate the FK. I thought that as long as I truncated the child table before the parent, I'd be okay without doing either of the options above, but I'm getting this error: Cannot truncate table 'TableName' because it is being referenced by a FOREIGN KEY constraint. John Rudy Correct; you cannot

Truncating all tables in a Postgres database

大兔子大兔子 提交于 2019-11-26 11:32:52
I regularly need to delete all the data from my PostgreSQL database before a rebuild. How would I do this directly in SQL? At the moment I've managed to come up with a SQL statement that returns all the commands I need to execute: SELECT 'TRUNCATE TABLE ' || tablename || ';' FROM pg_tables WHERE tableowner='MYUSER'; But I can't see a way to execute them programmatically once I have them. Henning FrustratedWithFormsDesigner is correct, PL/pgSQL can do this. Here's the script: CREATE OR REPLACE FUNCTION truncate_tables(username IN VARCHAR) RETURNS void AS $$ DECLARE statements CURSOR FOR SELECT

How to efficiently delete rows while NOT using Truncate Table in a 500,000+ rows table

こ雲淡風輕ζ 提交于 2019-11-26 10:35:51
问题 Let\'s say we have table Sales with 30 columns and 500,000 rows. I would like to delete 400,000 in the table (those where \"toDelete=\'1\'\" ). But I have a few constraints : the table is read / written \"often\" and I would not like a long \"delete\" to take a long time and lock the table for too long I need to skip the transaction log (like with a TRUNCATE ) but while doing a \"DELETE ... WHERE...\" (I need to put a condition), but haven\'t found any way to do this... Any advice would be

Truncate a string straight JavaScript

折月煮酒 提交于 2019-11-26 10:19:09
问题 I\'d like to truncate a dynamically loaded string using straight JavaScript. It\'s a url, so there are no spaces, and I obviously don\'t care about word boundaries, just characters. Here\'s what I got: var pathname = document.referrer; //wont work if accessing file:// paths document.getElementById(\"foo\").innerHTML = \"<a href=\'\" + pathname +\"\'>\" + pathname +\"</a>\" 回答1: Use the substring method: var length = 3; var myString = "ABCDEFG"; var myTruncatedString = myString.substring(0